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?

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
elmo330
  • 383
  • 2
  • 5
  • 12
  • Does it work? If not, what's wrong with it (be precise)? Do you just want style advice? – Jeffrey Bosboom Feb 20 '15 at 02:50
  • 3
    Your if expression should not have a semicolon after `== "on")` – Jagtesh Chadha Feb 20 '15 at 02: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 – elmo330 Feb 20 '15 at 02: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. –  Feb 20 '15 at 03:31
  • Try pressing F12. A new world of debugging awaits you. –  Feb 20 '15 at 03:33

4 Answers4

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

Oli Soproni B.
  • 2,774
  • 3
  • 22
  • 47
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>
Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71
varad mayee
  • 619
  • 7
  • 19
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";
    }
}
TResponse
  • 3,940
  • 7
  • 43
  • 63
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()">
Mayuresh Patil
  • 2,072
  • 1
  • 21
  • 44