Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • BS5 Tutorial
  • BS5 Interview Questions
  • BS5 Layout
  • BS5 Content
  • BS5 Components
  • BS5 Helpers
  • BS5 Utilities
  • BS4 Tutorial
  • BS Tutorial
  • Bootstrap Cheatsheet
  • Tailwind
  • CSS Frameworks
  • HTML Formatter
Open In App
Next Article:
How to add bootstrap toggle-switch using JavaScript ?
Next article icon

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:


Next Article
How to add bootstrap toggle-switch using JavaScript ?

S

Shreyasi_Chakraborty
Improve
Article Tags :
  • Web Technologies
  • HTML
  • Bootstrap
  • HTML-Misc
  • Bootstrap-4
  • Bootstrap-Misc

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences