Locating DOM elements using selectors
The Selectors API provides methods that make it quick and easy to retrieve
The NodeSelector interface
This specification adds two new methods to any objects implementing the
Returns the first matching
Elementnode within the node's subtree. If no matching node is found,
NodeListcontaining all matching
Elementnodes within the node's subtree, or an empty
NodeListif no matches are found.
querySelectorAll()is not live. This is different from other DOM querying methods that return live node lists.
You may find examples and details by reading the documentation for the
querySelectorAll() methods, as well as in the article Code snippets for querySelector.
The selector methods accept one or more comma-separated selectors to determine what element or elements should be returned. For example, to select all paragraph (
p) elements in a document whose CSS class is either
note, you can do the following:
var special = document.querySelectorAll( "p.warning, p.note" );
You can also query by ID. For example:
var el = document.querySelector( "#main, #basic, #exclamation" );
After executing the above code,
el contains the first element in the document whose ID is one of
You may use any CSS selectors with the
- Selectors API
- Code snippets for querySelector
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.