Skip to main content

Does the jQuery .unbind() method only work on jQuery created events?


I am trying to unbind all event handlers for all elements that are inside a particular container. Like a DIV. But those events have been bound/registered not using jQuery. Some are bound the manual way with onclick="...." or using regular native JavaScript.



But when I do something like this




$('#TheDivContainer').find('div,td,tr,tbody,table').unbind();



It does not appear to work. Which leads me to believe that the .unbind() only works if the events have been originally bound by jQuery.



Is that true? Is there another way of unbinding all events from a group of elements ?



Thanks!


Source: Tips4allCCNA FINAL EXAM

Comments

  1. You are right. As in the API:


    Any handler that has been attached
    with .bind() can be removed with
    .unbind().

    ReplyDelete
  2. Unbind will only work on jQuery created events as all methods that does this (addEventListener, and attachEvent) requires the both the node, the eventname, and the handler as an argument. bind takes care of storing these for you..

    By the way, DOM0 style event listerens (.foo = function(...) can only by removed by setting the same property to something else like null.

    ReplyDelete
  3. You could always do this:

    $('#TheDivContainer').find('div,td,tr,tbody,table')
    .unbind('click')
    .attr('onclick', ''); // edited to change null to ''


    etc. for all appropriate event types.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...