Knowledge Walls
John Peter
Pune, Maharashtra, India
How to toggle switch on and off event handlers in jquery with example
4787 Views
HTML
<div id="click-me-div" class="switched-on">
  click me
</div>
<br />
<button id="switch-off">Switch Off</button>
<button id="switch-on">Switch On</button>
JQuery
$(function(){
    $("body").on("click","#click-me-div[class*=switched-on]",function(){
        alert("I am On!");
    });
    $("button").on("click","",function(event){
       if ($(this).prop("id") == "switch-off"){
          $("#click-me-div").removeClass("switched-on");
       }
       else {
          $("#click-me-div").addClass("switched-on");
       }
    });
});
Demo & Output 
Previous Topics
Previous lessons of current book.
Computer software engineer articles of One day One Thing to Know
Computer software engineer articles of One day One Thing to Know
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details