tags:

views:

19

answers:

1

I want to use jQuery to remove a click event handler that wasn't added with jQuery. For example:

<a onclick="alert('This was set by onclick.')" href="#">Click me</a>

<script type="text/javascript">
  $(document).ready(function(){
    $("a").unbind(); // Why isn't this working?
    $("a").click(function(event){
      alert("This was set by jquery click.");
    });
  });
</script>

Is this possible? $("a").unbind() isn't working. The above code displays both alert() windows and not just the one added by jQuery. I think unbind() only unbinds handlers which were bound with bind().

+2  A: 

Try removeAttr instead:

$('a').removeAttr('onclick');
lonesomeday