Questa traduzione è incompleta. Collabora alla traduzione di questo articolo dall’originale in lingua inglese.

Restituisce il primo element all'interno del documento che corrisponde al selettore specificato o gruppo di selettori.

Note: The matching is done 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.


element = document.querySelector(selectors);


  • element is an Element object.
  • selectors is a string containing one or more CSS selectors separated by commas.


In this example, the first element in the document with the class "myclass" is returned:

var el = document.querySelector(".myclass");

Example: Powerful

Selectors can also be really powerful as demonstrated in the following example. Here, the first element <input name="login"/> within a <div class="user-panel main"> in the document is returned:

var el = document.querySelector("div.user-panel.main input[name='login']");


Returns null if no matches are found; otherwise, it returns the first matching element.

If the selector matches an ID and this ID is erroneously used several times in the document, it returns the first matching element.

Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.

querySelector() was introduced in the Selectors API.

The string argument pass to querySelector must follow the CSS syntax.

CSS Pseudo-elements will never return any elements, as specified in the Selectors API

To match ID or selectors that do not follow the CSS syntax (by using a colon or space inappropriately for example), you must escape the character with a back slash. As the backslash is an escape character in JavaScript, if you are entering a literal string, you must escape it twice (once for the JavaScript string, and another time for querySelector):

<div id="foo\bar"></div>
<div id="foo:bar"></div>

  console.log('#foo\bar');               // "#fooar" (\b is the backspace control character)
  document.querySelector('#foo\bar');    // Does not match anything

  console.log('#foo\\bar');              // "#foo\bar"
  console.log('#foo\\\\bar');            // "#foo\\bar"
  document.querySelector('#foo\\\\bar'); // Match the first div

  document.querySelector('#foo:bar');    // Does not match anything
  document.querySelector('#foo\\:bar');  // Match the second div


Specification Status Comment
Selectors API Level 2
The definition of 'document.querySelector()' in that specification.
Selectors API Level 1
The definition of 'document.querySelector()' in that specification.
Obsolete Initial definition

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1 (Yes) 3.5 8 10 3.2
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 (Yes) (Yes) 9 10.0 3.2

See also

Tag del documento e collaboratori

Hanno collaborato alla realizzazione di questa pagina: andrea.rizzo
Ultima modifica di: andrea.rizzo,