0

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"
}
1
  • Type: P1 !== p1 Commented Jan 8, 2019 at 13:51

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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

3 Comments

fixed but why i cant?
@Hamidreza place your function in another file, or add to <head> tag, don't forget to upvote and check as answer please.
@Hamidreza and upvote please ( for other ho has similar questions ), huge thanks!
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.