How to add bootstrap toggle-switch using JavaScript ? Last Updated : 14 Sep, 2020 Comments Improve Suggest changes Like Article Like Report Bootstrap toggle-switch is a simple component used for activating one of the two given options. Commonly used as an on/off button. The CSS framework is a library that allows for easier, more standards-compliant web design using the Cascading Style Sheets language. Bootstrap 4 is one such widely used CSS framework used by web developers for building user-friendly interactive web applications. Bootstrap along with JavaScript helps develop a responsive user interface. Approach: Bootstrap has an inbuilt data-toggle attribute that is used for toggling Bootstrap elements. Here bootstrapToggle() method is used for performing the task. The bootstrapToggle() method enables the toggling of the switch. If the switch is disabled and the user clicks the checkbox then it toggles to an enabled state. If the checkbox is in the enabled state then the bootstrapToggle() method toggles it to the disabled state. The code snippet below demonstrates a toggle switch using JavaScript. Example: html <!DOCTYPE html> <html> <head> <link rel="stylesheet" href= "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity= "sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <script src= "https://code.jquery.com/jquery.min.js"> </script> <script src= "https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"> </script> <link href= "https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet"/> </head> <body> <div class="container mt-5"> <!--Initialize toggles with id toggle-one with JavaScript.--> <input type="checkbox" id="toggle-one"> <script> $(function() { $('#toggle-one').bootstrapToggle({ on: 'Enabled', off: 'Disabled' }); }) </script> </div> </body> </html> Output: Alternate approach: The switch can also be toggled using individual buttons which when clicked trigger a JavaScript function.The JavaScript function toggles on or toggles off the switch depending on the previous state of the switch. Here, the toggleOn() method toggles on the switch whereas toggleOff() method toggles off the switch. Example: html <!DOCTYPE html> <html> <head> <link rel="stylesheet" href= "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity= "sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <script src= "https://code.jquery.com/jquery.min.js"> </script> <script src= "https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"> </script> <link href= "https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet"/> </head> <body> <div class="container mt-5"> <input id="toggle-trigger" type="checkbox" data-toggle="toggle"> <button class="btn btn-success" onclick="toggleOn()"> On Button </button> <button class="btn btn-danger" onclick="toggleOff()"> Off Button </button> </div> <script> function toggleOn() { $('#toggle-trigger').bootstrapToggle('on') } function toggleOff() { $('#toggle-trigger').bootstrapToggle('off') } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to add bootstrap toggle-switch using JavaScript ? S Shreyasi_Chakraborty Follow Improve Article Tags : Web Technologies HTML Bootstrap HTML-Misc Bootstrap-4 Bootstrap-Misc +2 More Similar Reads How to create Basic flip toggle switch using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a basic flip toggle switch using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel= 1 min read How to hide Bootstrap modal with JavaScript? This article will tell us how the bootstrap executes when the .modal (modal window) gets closed. At some point of time, the modal window â whenever it gets opened (along with the class modal), it is going to get closed.As soon the modal has got finished, after being getting hidden from the user, the 2 min read How to create Disable flip toggle switch using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Disable flip toggle switch using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=" 1 min read How to create a Theme flip toggle switch using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be creating a Theme flip toggle switch using jQuery Mobile. The theme can be specified using the "data-track-theme" and the "data-theme" at 1 min read How to create Fieldcontain flip toggle switch using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making Fieldcontain flip toggle switch using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link 1 min read How to create a radio button similar to toggle button using Bootstrap ? Toggle Buttons: The buttons that can change from one state to another, i.e. which can be toggled from on state to off state or vice-versa are called toggle buttons. For example: A particular switch in our house can either be on or off. This is a very good example of a real life toggle switch. The Wi 3 min read How to create Mini flip toggle switch using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a Mini flip toggle switch using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link rel=" 1 min read Bootstrap 5 Modal Via JavaScript Bootstrap 5 modal via JavaScript allows for the dynamic creation of models using JavaScript. Utilize Bootstrap's modal API to trigger modal display, content loading, and event handling for interactive and responsive user experiences.Prerequisite: Bootstrap 5 Modal Bootstrap 5 Modal Usage MethodsBoot 2 min read How to create Theme Disable flip toggle switch using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be making Theme Disable flip toggle switch using jQuery Mobile. Approach: First, add jQuery Mobile scripts needed for your project. <link 1 min read How to create Mini Disable flip toggle switch using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets, and desktops. In this article, we will be making a mini disabled flip toggle switch using jQuery Mobile. Approach: First, add jQuery mobile scripts needed for your project. <l 1 min read Like