Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

Element.querySelector Redirect 1

Summary

Returns the first element that is a descendant of the element on which it is invoked that matches the specified group of selectors.

Syntax

element = baseElement.querySelector(selectors);
  • element and baseElement are element objects.
  • selectors is 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.

The string argument pass to querySelector must follow the CSS syntax. See document.querySelector for a concrete examples.

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

Specification

See also

Document Tags and Contributors

 Last updated by: Sheppy,