hoverintent icon indicating copy to clipboard operation
hoverintent copied to clipboard

document.querySelectorAll doesn't seem to work

Open Dan503 opened this issue 8 years ago • 5 comments

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.

Dan503 avatar Jul 21 '16 00:07 Dan503

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 avatar Jul 23 '16 13:07 tristen

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

tristen avatar Jul 23 '16 13:07 tristen

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.

Dan503 avatar Jul 23 '16 14:07 Dan503

1+ On this. Would def make a difference 👍

lobenichou avatar Aug 10 '16 21:08 lobenichou

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');
    }
  );
}

danielpost avatar May 31 '19 15:05 danielpost