jQuery Event How to - Unbind all event and then add new event handler








Question

We would like to know how to unbind all event and then add new event handler.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.2.js'></script>
<script type='text/javascript'>
function testJQueryClick(){<!--  w ww .  j  a v a2  s  .  c o m-->
     $('#button2').unbind().click(function(){ 
         console.log ('Debug'); 
         return false; 
     });
}

</script>
</head>
<body>
  <input type="button" id="button1" value="Button 1" onclick="testJQueryClick(); return false" />
  <input type="button" id="button2" id="button2" value="Button 2" />
</body>
</html>

The code above is rendered as follows: