All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Toggle Button

Last Updated : Jul 1, 2023

JavaScript Toggle Button

In this tutorial we will show you the solution of JavaScript toggle button, here we are going to show you toggle button example. As we know toggle button allows the user to change a setting between two states.

We used conditional statements for toggle a button by changing value attribute with the help of button id.

For access button element by id we needs to use ‘getElementById()’ method.

Step By Step Guide On JavaScript Toggle Button :-

Here we defined input element button type with attributes ‘id, onclick event, value’.

When user clicks on IN-ACTIVE button then onclick event triggers btnToggle() method.

In btnToggle() method for access defined button we collected button id ‘tbtn’ by ‘getElementById()’ method and stored on variable ‘b’.

Using if condition we checking button value attributes value is whether ‘ACTIVE’ or ‘IN-ACTIVE’ then we changing that value to its reverse.

<!DOCTYPE html>
<html>
    <head>
        <title>TOGGLE BUTTON</title>
    </head>
    <body>
        <input type="button" id="tbtn" onclick="btnToggle();" value="IN-ACTIVE">
        <script>
            function btnToggle(){
                var b=document.getElementById("tbtn");
                if(b.value=="ACTIVE"){
                    b.value="IN-ACTIVE";
                }
                else if(b.value=="IN-ACTIVE"){
                    b.value="ACTIVE";
                }
        }
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined input element with attributes ‘type,id,onclick,value’ with respective values ‘button, tbtn, btnToggle(),IN-ACTIVE’.
  7. In script we defined function btnToggle(), here we collecting button input id ‘tbtn’ by getElementById() method for access input element and stored on variable ‘b’.
  8. In if condition we checking whether button input element’s value is ‘ACTIVE’ or not. If returns true means then we changing value attributes value to ‘IN-ACTIVE’ using ‘value’ statement.
  9. In else if condition we checking whether button input element’s value is ‘IN-ACTIVE’ or not. If returns true means then we changing value attributes value to ‘ACTIVE’ using ‘value’ statement.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how toggle a button using javascript.

When we executes program on browser we can see our defined button ‘IN-ACTIVE’ user needs to clicks on button then it’s value changed to ‘ACTIVE’ if again you done the same it changes to ‘IN-ACTIVE’ state, here we toggling button between ‘ACTIVE, IN-ACTIVE’ states.

If your wish to change specified toggle statement values or create toggle effect by using some other attributes then you can modify it accordingly.

I hope this tutorial on JavaScript toggle button helps you and the steps and method mentioned above are easy to follow and implement.