View Full Version : not getting text between p tags

11-29-2012, 07:38 AM
<title>JavaScript String toLowerCase() Method</title>
<script type="text/javascript">
//var lstr = str.toLowerCase();
//document.getElementById("demo").innerHTML = lstr;
<p id="demo">EARTH IS ROUND</p>

in above code i want to access string EARTH IS ROUND IN ALERT BOX

11-29-2012, 08:15 AM
Your javascript code is before the <p id="demo"> element, so it will be executed before this element even exists. So getElementById("demo") will not be able to find it and will fail.

1 - Move the <script> tag below the <p> element, right before the </body> OR
2 - Use window.onload = function() { // your code here} so that your code will only be executed after the page finished loading (including images and other external elements)

11-29-2012, 08:40 AM
alert is for debugging only - don't forget to remove it before your page goes live.