FadedForEver
FadedForEver

Reputation: 144

Cannot set property 'innerHTML' of null in JS

Code didn't work and still return 'f' world after function call, what's wrong with my code? thanks for help

HTML:

<html>
  <head>
    <script type="text/javascript" src="functions.js"></script>
  </head>
  <body onload="changeText()">
    <p id="P1">
      fu
    </p>
  </body>
</html>

JS:

function changeText(){
  document.getElementById("p1").innerHTML = "slm"
}

Upvotes: 0

Views: 128

Answers (4)

20yco
20yco

Reputation: 906

please try this one, problem was that you can't call function changeText():

<html>

  <head>
    <script type="text/javascript">
      function changeText(){
        document.getElementById("P1").innerHTML = "slm"
      }
    </script>
  </head>

  <body onload="changeText()">
    <p id="P1">fu</p>
  </body>

</html>

fiddle

Upvotes: 0

Jorge Piquer
Jorge Piquer

Reputation: 156

ID's in Javascript are case sensitive (all attributes are). The ID in the HTML is P1 but you're looking for p1 so it's not finding it.

Upvotes: 2

j_akshay
j_akshay

Reputation: 59

You are accessing the wrong ID Note P is capital letter, try:

document.getElementById("P1").innerHTML = "slm"

Upvotes: 0

E. Mancebo
E. Mancebo

Reputation: 754

Because the selector is case sensitive. Try to use P1 instead of p1: document.getElementById("P1").innerHTML = "slm"

Upvotes: 3

Related Questions