JavaScript · DOM and events

querySelector

Returns the first element matching a CSS selector, or null.

Syntax
document.querySelector(selector)

Example

Try it

Notes

Cache the result instead of querying on every event.

More in this group

addEventListener append classList closest createElement dataset element.style Event delegation

Learn this in the course

Reference pages are the quick answer. The course explains the why, shows the full example and ends with a challenge.

Open the course