4

I have an HTML page that contains many onmouseover events. These events are linked to a,img,td and div tags.

What is the most efficient Javascript way to remove all the onmouseover events, presuming the page has loaded.

I'm not using jQuery.

(the task is to remove unneeded tooltips when the page is accessed using touch devices)

soupagain
  • 1,123
  • 5
  • 16
  • 32

3 Answers3

1

Since all your event handlers point to the same central function - you can disable tooltips inside of that function.

If this function handles both onmouseover and onclick events - you can check event.type and disable tooltips only if it's equal mouseover.

Demo: http://jsfiddle.net/cLHgK/1/

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • Unfortunately I do need to remove the onmouseover events, as the iPhone is capturing them when trying to click on element, and processing them incorrectly. The only solution is to remove them entirely. – soupagain Sep 16 '13 at 22:00
  • Really? Even if your code specifically states to do nothing on mouseover and just return? Weird. – Yuriy Galanter Sep 16 '13 at 22:07
1

A simple function looping through all the elements should do the trick. This one does it for all divs:-

function removeMouseOver() {
    var divs = document.body.getElementsByTagName('div');
    for (var i = 0; i < divs.length; i++) {
        divs[i].onmoseover = function() {};
    }
}

another way is to define the mouseover tooltip function as a var

var tooltip = function(){// put your tooltip code here };
var donothing = function(){};

//to turn tooltips on:-
onmouseoverfunc = tooltip;

//to turn tooltips off:-
onmouseoverfunc = donothing;

in the elements

document.getElementById('mydiv').onmouseover = function(){mouseoverfunc();};
Paul Nelson
  • 137
  • 4
1

Just add a unique class to everything that has the mouseover event and then make a loop

get=document.getElementsByClassName("classhere");
for(i=0; i<get.length; i++){
    get[i].removeEventListener("mouseover", yourFunction, false);
   }

replace yourFunction and classhere, tell me if it works or not

y0ruba
  • 224
  • 1
  • 2
  • 9