YouTip LogoYouTip

Met Document Queryselector

# HTML DOM querySelector() Method [![Image 3: Document Object Reference](#) Document Object](#) ## Example Get the element with id="demo" in the document: document.querySelector("#demo"); [Try it Β»](#) ## Definition and Usage The querySelector() method returns the first element in the document that matches a specified CSS selector. **Note:** The querySelector() method only returns the first element that matches the specified selector. If you need to return all matching elements, use the querySelectorAll() method instead. For more CSS selectors, visit our (#) and our (#). * * * ## Browser Support The numbers in the table specify the first browser version that fully supports the method. | Method | | | | | | | --- | --- | --- | --- | --- | --- | | querySelector() | 4.0 | 8.0 | 3.5 | 3.1 | 10.0 | * * * ## Syntax document.querySelector(_CSS selectors_) ## Parameter Values | Parameter | Type | Description | | --- | --- | --- | | _CSS selectors_ | String | Required. Specifies one or more CSS selectors to match the elements. You can use their id, class, type, attributes, attribute values, etc. to select elements. For multiple selectors, separate them with a comma. Returns a matching element. **Tip:** For more CSS selectors, see our (#). | ## Technical Details | DOM Version: | Selectors Level 1 Document Object | | --- | | Return Value: | The first element that matches the specified CSS selector. Returns null if no match is found. Throws a SYNTAX_ERR exception if an invalid selector is specified. | * * * ## More Examples ## Example Get the first

element in the document: document.querySelector("p"); [Try it Β»](#) ## Example Get the first element with class="example": document.querySelector(".example"); [Try it Β»](#) ## Example Get the first

element with class="example": document.querySelector("p.example"); [Try it Β»](#) ## Example Get the first element with a "target" attribute: document.querySelector("a"); [Try it Β»](#) ## Example The following example demonstrates the use of multiple selectors. Assume you have selected two selectors:

and

elements. The following code will add a background color to the first

element in the document:

A h2 element

A h3 element

document.querySelector("h2, h3").style.backgroundColor = "r

← Prop Html ContenteditableJs Strings β†’