I suppose the answer to this question depends on what browser you are using, but I guess that just makes it all the more interesting.
I'm wondering how the querySelector()
method is actually implemented. Similarly, I'm curious about querySelectorAll()
and other methods like getElementById()
and getElementByClassName()
etc.
Is it a depth first search, breadth first search, or does it utilize some auxiliary data structure, like a global hash table to act as a registry?
The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.
The querySelector() method in HTML is used to return the first element that matches a specified CSS selector(s) in the document. Note: The querySelector() method only returns the first element that matches the specified selectors. To return all the matches, use the querySelectorAll() method.
querySelectorAll() The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.
querySelector() and querySelectorAll() are two jQuery functions which helps the HTML elements to be passed as a parameter by using CSS selectors ('id', 'class') can be selected.
querySelector: Returns the first element within the document (using depth-first pre-order traversal of the document's nodes|by first element in document markup and iterating through sequential nodes by order of amount of child nodes) that matches the specified group of selectors.
querySelectorAll: Returns a list of the elements within the document (using depth-first pre-order traversal of the document's nodes) that match the specified group of selectors. The object returned is a NodeList.
getElementById: Returns a reference to the element by its ID; the ID is a string which can be used to identify the element; it can be established using the id attribute in HTML, or from script.
as an ID should be unique - there's no question of order
getElementsByClassName: Returns an array-like object of all child elements which have all of the given class names. When called on the document object, the complete document is searched, including the root node. You may also call getElementsByClassName() on any element; it will return only elements which are descendants of the specified root element with the given class names.
querySelectorAll
is well documented here:
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
Returns a list of the elements within the document (using depth-first pre-order traversal of the document's nodes) that match the specified group of selectors. The object returned is a NodeList.
On the other hand, the documentation for getElementsByClassName
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
does not have the same assurance.
In fact, I've run into trouble with that on some older browsers -- with things being returned in an undermined fashion on various browsers. Though, this dates back to IE6 and may be stabilized under the HTML5 doctype.
If you MUST, 100%, ensure the document order, there is always the old walkTheDom code.
Recursion down DOM tree
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With