jQuery Method How to - Use off() method to remove event handler








Question

We would like to know how to use off() method to remove event handler.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!-- w  w  w . j a va2s.c o  m-->
    $('.clickable').click(function() {
        console.log('clicked');
        $('.clickable').off('click');
    });
});
</script>
</head>
<body>
  <div id="a" class="clickable">Div a</div>
  <div id="b" class="clickable">Div b</div>
  <div id="c" class="clickable">Div c</div>
</body>
</html>

The code above is rendered as follows: