How to show a javascript 'var' in my jsp?
...
<script type="text/javascript">
... // My code to get the value.
var val = combo.getValue();
</script>
<body>
The value is : //to be displayed here
</body>
How to show a javascript 'var' in my jsp?
...
<script type="text/javascript">
... // My code to get the value.
var val = combo.getValue();
</script>
<body>
The value is : //to be displayed here
</body>
You need to place it somewhere so it could be pushed to your JSP page. A hidden field is a good option:
Other possibility might be using AJAX.
One way or another, I would suggest you reading a little bit more on general topics about Web Applications to differentiate JSP/JavaScript/POST/GET/CSS/HTML and other basic concepts.
Good luck!
Add a HTML element which should mark the place where the value is to be displayed and give it an
id
.Then let your JS access it by
document.getElementById()
and modify its inner HTML.You only need to ensure that the particular script executes after the HTML page has loaded. Do it during
window.onload
or put the<script>
at end of<body>
or wrap it in afunction
which you execute on some event.As to the JSP part, this is not relevant here. All JSP does is generating and sending HTML/CSS/JS code from webserver to webbrowser. JavaScript knows nothing about JSP, all it can see and access is HTML/CSS which you can also see by rightclicking the page in webbrowser and choosing View Source.
See also: