Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
156
rated 0 times [  163] [ 7]  / answers: 1 / hits: 87989  / 14 Years ago, tue, august 10, 2010, 12:00:00

What I mean is, can a variable/array declared and initialized be used in HTML, outside the <script>-tags? Fx.



<script type=text/javascript>
var foo = array('placeholder1', 'placeholder2');
</script>

<body>
<p><!--access the variable here-->foo[0]</p>
</body>


How do you access the variable/array in this case? like this:



<p><script type=text/javascript>document.print(foo[0])</script></p>


??


More From » arrays

 Answers
6

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>

[#95968] Saturday, August 7, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jase

Total Points: 442
Total Questions: 107
Total Answers: 94

Location: Sao Tome and Principe
Member since Wed, Dec 29, 2021
2 Years ago
;