Can a JavaScript variable be used in plain HTML?

Two ways to do this. This is the better one:

<script type="text/javascript">
// make sure to do this onLoad, for example jQuery's $()
var foo = array('placeholder1', 'placeholder2');
document.getElementById("fooHolder").innerHTML = foo.toString();
</script>
...
<p id="fooHolder"></p>

Or you could do it this way (which, as Marcel points out, doesn’t work in XHTML and really shouldn’t be used anyway):

<p><script type="text/javascript">document.write(foo)</script></p>

Leave a Comment