Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    not getting text between p tags

    <html>
    <head>
    <title>JavaScript String toLowerCase() Method</title>
    </head>
    <body>
    <script type="text/javascript">
    alert(document.getElementById("demo").value);
    //var lstr = str.toLowerCase();
    //document.getElementById("demo").innerHTML = lstr;
    </script>
    <p id="demo">EARTH IS ROUND</p>
    </body>
    </html>

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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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.

    Solutions:
    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)
    Last edited by devnull69; 11-29-2012 at 08:07 AM.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    alert is for debugging only - don't forget to remove it before your page goes live.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •