royco
royco

Reputation: 5529

Use jQuery to unbind a click handler that wasn't added with jQuery?

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().

Upvotes: 1

Views: 102

Answers (1)

lonesomeday
lonesomeday

Reputation: 237827

Try removeAttr instead:

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

Upvotes: 3

Related Questions