3 ms·
Sometimes this helper function will do everything that I would want jQuery for: var _$ = function(selector){ var els = document.querySelectorAll(selector
by zachrose 11y ago
Sometimes this helper function will do everything that I would want jQuery for:
var _$ = function(selector){
var els = document.querySelectorAll(selector);
return Array.prototype.slice.call(els);
}
For example:
_$('span').forEach(function(el){ el.style.color = 'red' });
- awalGarg 11y agoAlso: DOM Level 4[1] adds the iterator protocol to NodeList, so soon you won't need that slice.call line. Simply return the result of querySelectorAll ;-) Works in Chrome canary already IIRC. 1: https://www.w3.org/TR/dom/#interface-nodelist https://www.w3.org/TR/dom/#interface-nodelist
- VeejayRampay 11y agoWhy have we had to wait so long though? Who in their right mind thought that it'd be a good idea for NodeList instances to NOT be iterable in the first place? It's almost people writing those specs never use their interfaces for actual work.
- kowdermeister 11y agoI reacted the same way. First I thought 'o yeah, pretty neat' then 'wait, what? it's a collection, but I can't use foreach?' finally arrived at 'ok, I just pull in jQuery again' :)