Unbinding inline onClick not working in jQuery?

Posted by Polaris878 on Stack Overflow See other posts from Stack Overflow or by Polaris878
Published on 2010-05-04T18:51:16Z Indexed on 2010/05/04 18:58 UTC
Read the original article Hit count: 342

Filed under:
|

Okay so, I'm wondering how to unbind an inline onclick event in jQuery. You'd think .unbind() would work, however it doesn't.

To test this for yourself, play around with the following HTML and JavaScript:

function UnbindTest() {
    $("#unbindTest").unbind('click');
}

function BindTest() {
    $("#unbindTest").bind('click', function() { alert("bound!"); });
}


<button type="button" onclick="javascript:UnbindTest();">Unbind Test</button>
<button type="button" onclick="javascript:BindTest();">Bind Test</button>
<button type="button" onclick="javascript:alert('unbind me!');" id="unbindTest">Unbind Button</button>

As you can see, unbinding does not unbind the inline onclick event... however it does unbind the click event added with bind().

So, I'm wondering if there is a way to unbind inline onclick events short of doing the following:

$("#unbindTest").get(0).onclick = "";

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery