jQuery > Events

Attach & detach event in jQuery

How to attach and detach an event to and from an element in jQuery?


To attach and detach an event to and from an element, bind() and unbind() method can be used respectively.

<script>
       function bindAction() {
           alert("I am active !");
       }

       $("#btnBindAction").click(function () {
           $("#btnAction").bind('click', bindAction).val("Click Now");
       });

       $("#btnUnbindAction").click(function () {
           $("#btnAction").unbind('click', bindAction).val("Simple Button");
       });
  </script>

In above code snippet first we have declared a function called “bindAction” that will be bounded to the “btnAction” when “btnBindAction” element (having id value) will be clicked and the same function will be unbounded when “btnUnbinAction”  (having id value) element will be clicked.

 Views: 1583 | Post Order: 64



Write for us