query
query is a polyfill for the query and queryAll methods and the Elements collection from the DOM Living Standard.
query is a context-aware version of querySelector, and Elements is a collection of nodes that can also run Array methods.
First Heading Subsection Heading
main = document; // querySelector has an unexpected quirkmain; // returns the first heading because it technically matches // query works as expectedmain; // returns the subsection heading because it is context-aware
This is extremely useful in context-sensitive situations.
Section 1 Section 1-1
document;
Methods
queryAll
Returns a non-live NodeList of all descendent elements that match the relative CSS selectors.
elementList = baseElement;
query
Returns the first descendent element that matches the relative CSS selectors.
element = baseElement;
Browser compatibility
query
and queryAll
will work in Android 2.1+, Blackberry 7+, Chrome, Firefox 3.5+, Internet Explorer 8+, iOS Safari 3.2+, Opera 10+, and Safari 3.1+.
Polyfill status
If you like query and queryAll and would like to use it natively, convince Chrome, Opera, Firefox, or Safari to implement it.
query.js is 3.98KB or 570B minified + gzipped.
query.legacy.js is 4.29KB or 614B minified + gzipped.