0

I am Struck at simple code , getelementbyid.innerHtml in html page. Please help me.

<!doctype html>
<html lang="en">
 <head>
  <title>Dom examples</title>
  <script>
   var code = document.getElementById("one").innerHTML;
   code = code + " !!";
   console.log(code);
  </script>
 </head>
 <body>
  <h4> DOM practise page </h4>
  <p id="one"> This page is for practise of DOM model in JS</p>
  <p id="two"> Thank You </p>
 </body>
</html>

7 Answers7

0

Your javascript is called before your browser render your HTML page. So when your browser call getElementById the ID isn't defined.

Just move your javascript at the end of your file

sheplu
  • 2,754
  • 2
  • 19
  • 19
0

Just move the script below... the causing is beacause the id isnt render yet.

<!doctype html>
<html lang="en">
 <head>
  <title>Dom examples</title>
  
 </head>
 <body>
  <h4> DOM practise page </h4>
  <p id="one"> This page is for practise of DOM model in JS</p>
  <p id="two"> Thank You </p>
 </body>
    <script>
   var code = document.getElementById("one").innerHTML;
   code = code + " !!";
   console.log(code);
  </script>
</html>
Jesus Carrasco
  • 1,356
  • 1
  • 8
  • 15
0

Utilize onload property of window to call a function when window has loaded

<!doctype html>
<html lang="en">

<head>
  <title>Dom examples</title>
  <script>
    onload = function() {
      var code = document.getElementById("one").innerHTML;
      code = code + " !!";
      console.log(code);
    }
  </script>
</head>

<body>
  <h4> DOM practise page </h4>
  <p id="one"> This page is for practise of DOM model in JS</p>
  <p id="two"> Thank You </p>
</body>

</html>
guest271314
  • 1
  • 10
  • 82
  • 156
0

just put the script part before close body and it will work.

<!doctype html>
<html lang="en">
    <head>
        <title>Dom examples</title>
    </head>
    <body>
        <h4> DOM practise page </h4>
        <p id="one"> This page is for practise of DOM model in JS</p>
        <p id="two"> Thank You </p>

        <script>
            var code = document.getElementById("one").innerHTML;
            code = code + " !!";
            console.log(code);
        </script>
    </body>
</html>
Fr4NgUs
  • 470
  • 6
  • 9
0

Browsers parses html page from top to down. So, if you put your script in the head, it will get rendered first and then your body. So your script is searching for an id which is yet not rendered to the DOM.

By placing script at the bottom, it solves your problem.

<!doctype html>
<html lang="en">
 <head>
  <title>Dom examples</title>
 </head>
 <body>
  <h4> DOM practise page </h4>
  <p id="one"> This page is for practise of DOM model in JS</p>
  <p id="two"> Thank You </p>
    <script>
   var code = document.getElementById("one");
   code = code.innerHTML + " !!";
   console.log(code);
  </script>
 </body>
</html>
Anurag Singh Bisht
  • 2,320
  • 2
  • 16
  • 24
0

It is working fine just use script tag in end. For more clearity check. https://jsfiddle.net/djmayank/Lzewxgyw/

HTML

<head>
        <title>Dom examples</title>

    </head>
    <body>
        <h4> DOM practise page </h4>
        <p id="one"> This page is for practise of DOM model in JS</p>
        <p id="two"> Thank You </p>
    </body>

JS

var code = document.getElementById("one").innerHTML;
        code = code + " !!";
        console.log(code);
djmayank
  • 350
  • 2
  • 17
0

The script is loaded before your element with the id of one is loaded, therefore one does not exist yet. You have the following options:

  1. You put the script after the tag you need: <!doctype html> <html lang="en"> <head> <title>Dom examples</title> </head> <body> <h4> DOM practise page </h4> <p id="one"> This page is for practise of DOM model in JS</p> <p id="two"> Thank You </p> <script> var code = document.getElementById("one").innerHTML; code = code + " !!"; console.log(code); </script> </body> </html>
  2. Make a function and use the onload attribute, which is preferable, as the code will be reusable for other features: <!doctype html> <html lang="en"> <head> <title>Dom examples</title> <script> function myFunction() { var code = document.getElementById("one").innerHTML; code = code + " !!"; console.log(code); } </script> </head> <body> <h4> DOM practise page </h4> <p id="one" onload="myFunction()"> This page is for practise of DOM model in JS</p> <p id="two"> Thank You </p> </body> </html>
Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148