I’d like to refer to a variable (“special”) in field later in the same script. I’ve gotten the variable to display with alert boxes and document.write, but don’t now how to make to apply its value to the value field in
var special=(10000-health);
var health=(100);
<input style="background:#FF7777;" readonly="readonly" type="text" value="special" id="special" />
this just writes “special” to the box, when I would like the value instead.
You have to set the value explicitly:
Note: You can only access the element after it was parsed in the DOM. To be sure, you can insert this part of the script after the element in the HTML. Often JavaScript code is added just before the closing
bodytag or is only executed when theloadevent fires. For more information, see Where to place JavaScript in a HTML file.Update: Here is an example:
References:
getElementById,DOMMDC’s JavaScript Guide is also worth reading.