Question:

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
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Step 1: document.querySelector() takes a CSS selector and returns a single element.

Step 2: The selector ".class" matches elements by class name. When several elements share the class, querySelector returns only the first match in document order.

Step 3: To get every match you would use querySelectorAll(), which returns a NodeList. querySelector never throws for multiple matches and does not require an ID.

Step 4: So it selects the first element with the given class name.
Was this answer helpful?
0
0