Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (1.5k points)

I am trying to make a simple toggle button in javascript. However, the button will only turn "OFF" and will not turn back "ON"

<html><head></head>
<script type="text/javascript">
function toggle(button)
{
  if(document.getElementById("1").value=="OFF"){
   document.getElementById("1").value="ON";}

  if(document.getElementById("1").value=="ON"){
   document.getElementById("1").value="OFF";}
}
</script>
<body>
<form action="">
<input type="button" id="1" value="ON" style="color:blue"
       o

Please help

1 Answer

0 votes
by (1.4k points)

To create a simple toggle button just copy paste the following code and you should be good to go: 

<html> 

    <head> 

        <script type="text/javascript"> 

            function toggle(button) 

            { 

              if(document.getElementById("1").value=="OFF") 

              { 

               document.getElementById("1").value="ON"; 

              } 

              else 

              { 

                document.getElementById("1").value="OFF"; 

              } 

            } 

        </script> 

    </head> 

    <body> 

        <form action=""> 

            <input type="button" id="1" value="ON" style="color:blue" onclick="toggle(this);"> 

        </form> 

    </body>  

</html> 

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...