Select HTML elements by CSS selectors in vanilla JavaScript
Question:
How to select HTML elements by CSS selectors in vanilla JavaScript? Answer:
const elements = document.querySelectorAll("div.article > h1");
Description:
You don't need jQuery to find HTML elements with CSS selectors. With the querySelectorAll
vanilla JavaScript method, you can get the same result.
The querySelectorAll()
method returns a static NodeList representing a list of the document's elements that match the specified group of selectors. The selector must be a valid CSS selector string.
Reference:
The querySelectorAll reference
Share "How to select HTML elements by CSS selectors in vanilla JavaScript?"
Related snippets:
- Select HTML elements by CSS selectors in vanilla JavaScript
- Play video in fullscreen with vanilla JavaScript
- Exit from fullscreen mode on click in JavaScript
- Toggle fullscreen and normal mode with JavaScript
- Switch browser to fullscreen mode with JavaScript
- Get the value of text input field in JavaScript
- Get element with data- attribute using vanilla JavaScript
- Get the value of selected radio button in JavaScript
- Get selected option from select using JavaScript
- Check if element is hidden in JavaScript
Tags:
select, html, element, query, querySelector, querySelectorAll, css, selector Technical term:
Select HTML elements by CSS selectors in vanilla JavaScript