Question:medium

What does document.querySelector(".class") do?

Show Hint

The plain querySelector returns one node; which one when many match?
Updated On: Jul 2, 2026
  • Throws an error if multiple elements have the same class
  • Selects all elements with the given class name
  • Selects the first element with the given class name
  • Selects an element only if it has an ID
Show Solution

The Correct Option is C

Solution and Explanation

The method $\texttt{querySelector}$ is the single-result CSS lookup. Its argument is any CSS selector, and a leading dot means match by class.

If the page has many elements with that class, $\texttt{querySelector}$ still hands back just one, the first it meets while scanning the document. For the full set you need the sibling method $\texttt{querySelectorAll}$.

It does not raise an error on duplicates and does not care about IDs, so the correct behavior is returning the first matching element.

\[\boxed{\text{Selects the first element with the given class name}}\]
Was this answer helpful?
0