4

Only started JS a couple of days ago, and I'm already having some troubles getting a toggle to work. I want the button to toggle between on and off when clicked.

function click() {
  var change = document.getElementById("toggle");
  if (change.innerHTML == "on"); {
    change.innerHTML = "off";
  } else {
    change.innerHTML = "on";
  }
}
<button type="button" id="toggle" onClick="click()">on</button>

Is this how I should go about it?

5
  • Does it work? If not, what's wrong with it (be precise)? Do you just want style advice? Commented Feb 20, 2015 at 2:50
  • 3
    Your if expression should not have a semicolon after == "on") Commented Feb 20, 2015 at 2:52
  • @JeffreyBosboom it's not working at all unfortunately! I just want to know why that may be. I thought I may be missing something someone could spot out Commented Feb 20, 2015 at 2:54
  • If you open your console, you will see a syntax error, most likely on the else. Did you open the console? Do you know how to open the console? You should learn that before you do anything else. Commented Feb 20, 2015 at 3:31
  • Try pressing F12. A new world of debugging awaits you. Commented Feb 20, 2015 at 3:33

4 Answers 4

4

try this

<!DOCTYPE html>
<html>
    <body>

        <button id="toggle" onclick="myFunction()">on</button>

        <script>

            function myFunction() {
                var change = document.getElementById("toggle");
                if (change.innerHTML == "on")
                {
                    change.innerHTML = "off";
                }
                else {
                    change.innerHTML = "on";
                }
            }

        </script>
    </body>
</html>

define your function to be unique as always and not make use of the javascript function/reserve words just a recommendation/suggestion

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

Comments

1

Your having mistake in the if statement,there is no semicolon after if statement write the code as like below

 <button name="toggle" id="toggle" onclick="myFunction()">on</button>

            <script>

                function myFunction() {
                    var change = document.getElementById("toggle");
                    if (change.innerHTML == "on")
                    {
                        change.innerHTML = "off";
                    }
                    else {
                        change.innerHTML = "on";
                    }
                }

            </script>

Comments

0

You can do this:

<button type = "button" id= "toggle" onClick = "click()">on</button>


function click()
{
    var change = document.getElementById("toggle");
    if (change.value == "on")
    {
        change.value = "off";
    }
    else 
    {
      change.value = "on";
    }
}

or by doing this:

function click()
{
    if (this.value=="on")
    {
        this.value = "off";
    }
    else 
    {
         this.value = "on";
    }
}

2 Comments

You've left in the spurious semi-colon after the first if statement, which will cause the parser to choke.
Ah yes right you are - I have corrected it - just a typo!
0

It will work for you

function myFunction()
    {
        var change = document.getElementById("toggle");
        if (change.value=="off") change.value = "on";
        else change.value = "off";
    }

for button

<input type="button" value="on" id="toggle" onclick="myFunction()">

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.