0

Hello friends I am new to learning HTML I am trying to make a basic website but I can't get a button which changes the sites background and the button text value to work please help!

<!DOCTYPE html>
<html>
  <body>

    <script type="text/javascript">
      function lightsOff()
      {
        if (document.getElementbyID("btnLights").value == "Lights Off") {
          document.body.style.backgroundColor = "#151515";
          document.getElementbyID("btnLights").value = "Lights On";
        }
        else {
          document.body.style.backgroundColor = "#FFFFFF";
          document.getElementbyID("btnLights").value = "Lights Off";
        }
      }
    </script>

  <center>

    <input type="button" id="btnLights" onclick="lightsOff()" value="Lights Off"></input>
  </center>

</body>
</html>

I'm sure its just something silly but I am new and need help!

4
  • 2
    The right way is: getElementById. Javascript is case sensitive. You're probably getting errors in the console. Check it. Commented Oct 17, 2013 at 18:21
  • This was it! Thanks so much I shouldn't have copied the code from a different answer, I'm not using anything with a console just notepad. Commented Oct 17, 2013 at 18:23
  • No probs. I've posted an answer. Commented Oct 17, 2013 at 18:26
  • I recommend finding a good IDE or Programmers notepad. It helps find errors such as that :-) Commented Oct 17, 2013 at 18:32

4 Answers 4

2

You are almost there, Just replace getElementbyID with getElementById

Here you have a DEMO

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

Comments

0

1) getElementById is the proper syntax, not ID

2) Not sure on this, but you may need your script to be underneath your HTML elements, so that they exist when it is parsed by the browser for the first time. Someone can correct me on this though if it's inaccurate.

Comments

0

Javascript is case sensitive. Just replace getElementbyID with getElementById and things will just work.

Comments

0

you have done just smaller mistake of spelling.

here : document.getElementbyID //your code

correct syntex is : document.getElementById // correct code

replace "document.getElementbyID" by this "document.getElementById"

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.