Reputation: 61
<script>
document.getElementById("comment").innerHTML="<?php echo 'foo bar'; ?>";
</script>
<div id="comment"></div>
This should give the word "foo bar" in the div element,but its not.Do not know where i'm going wrong. Please help.
Upvotes: 0
Views: 238
Reputation: 401182
You are trying to use the comment
element :
document.getElementById("comment").innerHTML="...";
Before it is declared :
<div id="comment"></div>
And firebug shows the following error :
(source: pascal-martin.fr)
You should :
If you choose the first solution, your code will look like this :
<div id="comment"></div>
<script>
document.getElementById("comment").innerHTML="<?php echo 'foo bar'; ?>";
</script>
And it will work :
foo bar
string will be visile
(source: pascal-martin.fr)
Upvotes: 2
Reputation: 78046
The script needs to go below the div, as it's trying to populate a div that hasn't been inserted into the DOM yet.
Upvotes: 1