Click to See Complete Forum and Search --> : is it possible to call Javascript Variables in HTML?


Rashar
01-09-2004, 02:30 PM
Hi.

Is it possible to call a javascript variable into HTML code?

For example, I have a javascript function that add's numbers, and the results are passed to a variable called called var total. I want to take that variable, and call it in the HTML code to display the results. I can display the results if it is in a text box, however i would rather just display the value.

Thanks,

Rashar.

fredmv
01-09-2004, 02:35 PM
In the Netscape 4.x days, you used to be able to do something like this:<input type="text" value="&{var};" />Of course, this syntax no longer works (no idea why they got rid of it; it was rather useful). However, you can simply set the value property of the form element to a JavaScript variable to emulate this.

jaegernaut
01-09-2004, 02:47 PM
You can write out the value without having a form field by including this code in the HTML where you want it to show:

<script type="text/javascript">
document.write(variable);
</script>

In order for this to work you will have to have "variable" as a global variable so that it can be access outside of a function.

As an inline example:

<p>
The value of this variable is
<script type="text/javascript">
document.write(variable);
</script>
</p>

dschou_us
01-09-2004, 07:22 PM
you can use the "element.innerHTML=jsvar" to display the javascript value in html. Where element is the object that you obtained by using document.getElementById method.

Hope this help.