Table of contents
- 1. Syntax
- 2. Example
- 3. Notes
- 4. Browser compatibility
- 5. Specification
- 6. See also
Summary
Returns the first element that is a descendent of the element on which it is invoked that matches the specified group of selectors.
Syntax
element = baseElement.querySelector(selectors);
where
elementandbaseElementare element objects.selectorsis a group of selectors to match on.
Example
In this example, the first style element which either has no type or has type text/css in the HTML document body is returned:
var el = document.body.querySelector("style[type='text/css'], style:not([type])");
Notes
Returns null if no matches are found; otherwise, it returns the first matching element.
Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.
querySelector() was introduced in the WebApps API.
Browser compatibility
| Browser | Support | Notes |
|---|---|---|
| Internet Explorer | 8 | |
| Firefox (Gecko) | 3.5 (1.9.1) | |
| Opera | 10 | |
| Chrome | 1 | |
| Safari (webkit) | 3.2 (525.3) | webk.it/16587 |
Mozilla Developer Network