4 ms·
It sounds like you're looking for the querySelector and querySelectorAll methods: https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelector https:
by ordinary 13y ago
It sounds like you're looking for the querySelector and querySelectorAll methods: https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelector https://developer.mozilla.org/en-US/docs/Web/API/Document.qu...
- wwweston 13y agoquerySelector and querySelectorAll could have been pretty great, but they suffer from some serious problems as they ended up spec'd. The one I hate most is that qSA returns Yet Another JavaScript Collection That Is A Lot Like An Array But Isn't An Array And Therefore Doesn't Have Any Of The Nice Methods(TM). Yes, you're going to have to iterate over the thing using an index in a loop. There's others: http://ejohn.org/blog/thoughts-on-queryselectorall/ http://ejohn.org/blog/thoughts-on-queryselectorall/ I'm really not sure how the standards-makers got this so terribly wrong given that the libraries had already started to solve the problem.
- nostrademons 13y agoElements is supposed to fix this: http://dom.spec.whatwg.org/#elements http://dom.spec.whatwg.org/#elements But AFAICT isn't implemented anywhere, which makes it not that useful. Perhaps a polyfill could be done.
- lowboy 13y agoArray.prototype.slice.call(document.querySelectorAll('.myClass'), 0); Or if you want to abstract that into a utility function: function qsa(sel) { return Array.prototype.slice.call(document.querySelectorAll(sel), 0); } qsa('.myClass'); // Returns array of elements
- talmand 13y agoAnd now you are on the way to rewriting jQuery from scratch. Add a few more helper functions, separate it all out to its own js file, reuse the js file in other projects, and soon people will complain about the bloated js file you keep using in your projects. Man, I really do enjoy my chosen line of work. It creates such fun things to debate over.