tristen / hoverintent

:mouse: Fire mouse events when a user intends it
https://tristen.ca/hoverintent/
MIT License
331 stars 54 forks source link

document.querySelectorAll doesn't seem to work #17

Open Dan503 opened 8 years ago

Dan503 commented 8 years ago

I have this code

var _hoverTriggers = document.querySelectorAll('.JS-navigator__trigger--hover');

hoverintent(_hoverTriggers,
    function() {
        console.log('in');
    }, function() {
        console.log('out');
    }
);

And I am receiving this error:

Uncaught TypeError: e.addEventListener is not a function

The most common use for this plugin is triggering mega menus on navigation items. It doesn't make sense not supporting query selector all in this context.

tristen commented 8 years ago

querySelectorAll returns a NodeList which isn't a type handled internally. You would need to iterate through the list and apply hoverintent to each element respectively. i.e

var _hoverTriggers = document.querySelectorAll('.JS-navigator__trigger--hover');

Array.prototype.forEach.call(_hoverTriggers, function(el) {
  hoverintent(el, hoverInFunc, hoverOutFunc);
});
tristen commented 8 years ago

It might be a good idea to support NodeList though so I'll leave it open!

Dan503 commented 8 years ago

Yuk! XP

This issue drove me back to the jQuery version of the plugin. You need to support node list if you want to be able to compete with it.

lobenichou commented 8 years ago

1+ On this. Would def make a difference 👍

danielpost commented 5 years ago

For what it's worth, it's not that hard to make hoverIntent work with querySelectorAll:

const toggles = document.querySelectorAll('[data-dropdown-open]');

if (!toggles) return;

for (const toggle of toggles) {
  hoverIntent(
    toggle,
    () => {
      toggle.setAttribute('data-dropdown-open', 'true');
    },
    () => {
      toggle.setAttribute('data-dropdown-open', 'false');
    }
  );
}