Remove onmouseover event from all <img> tags

I do like this:

$.each($('img'), function () {
    this.unbind('onmouseover');
});

This does not work. Why?

+3
source share
2 answers

Try for example

$('img').unbind('mouseover');

No need to go in cycles .. and also should be mouseovernotonmouseover

Assumptions: You use .binda handler to bindmouseover

I do not use bind. some images have an onmouseover attribute, and I want to delete them. I am trying $ ('img'). RemoveAttr ('onmouseover'), but it still doesn't work

  • Using an inline event handler is not standard.
  • Since you are using jQuery, you must bind the handler as shown below.

The code:

$('img').on('mouseover', function () {
     //Your code
});

And later, you can untie them using .off

$('img').off('mouseover');

, ( ), ()

$.each($('img'), function () {
    $(this).removeAttr('onmouseover');
});
+7

:

$('img')
    .unbind('mouseover')               //remove events attached with bind
    .off('mouseover')                  //remove events attached with on
    .die('mouseover');                 //remove events attached with live
    .each(function(i,el){              //and for each element
        el.onmouseover = null          //replace the onmouseover event
    });
+5

All Articles