Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove onmouseover event from all <img> tags

I doing this way:

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

This does not work. Why?

like image 604
karaxuna Avatar asked May 24 '12 14:05

karaxuna


1 Answers

Try like below,

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

No need for looping.. and also it should be mouseover not onmouseover

Assumptions: You are using .bind to bind the mouseover handler

I'm not using bind. some images have onmouseover attribute and I want to delete them. I tries $('img').removeAttr('onmouseover') but it still does not work

  1. Using inline event handler is not a standard.
  2. Since you are using jQuery, you should bind handler like below.

Code:

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

And later can unbind them by using .off ->

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

A work around for what you have (not preferred), (Reference)

$.each($('img'), function () {
    $(this).removeAttr('onmouseover');
});
like image 171
Selvakumar Arumugam Avatar answered Oct 06 '22 00:10

Selvakumar Arumugam