Syntax

JavaScript Select an Element Syntax

In JavaScript, select an element: finds the first element matching a CSS selector — document.querySelector(".btn")

Quick Answer

Syntax
document.querySelector(".btn")
JavaScriptSyntax

Select an Element

Code example
document.querySelector(".btn")

Finds the first element matching a CSS selector.

Learn More

How to select an element in JavaScript

  1. Open your JavaScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

JavaScript Select an Element Syntax

Use document.querySelector(".btn").

Related Syntax References

Frequently asked questions

What is the JavaScript syntax for select an element?

Use this pattern: document.querySelector(".btn")

Can I copy this JavaScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this JavaScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify