querySelector method
The querySelector method allows
you to get a page element by an arbitrary
CSS selector. With the resulting element,
it will be possible to perform various
manipulations: change its text, attributes,
CSS styles, and so on.
Syntax
document.querySelector(selector);
Example
Let's get the paragraph #elem
and change its text with
textContent:
<p id="elem">text</p>
let elem = document.querySelector('#elem');
elem.textContent = '!!!';
The code execution result:
<p id="elem">!!!</p>
Example
Let's get the paragraph with the class
elem and change its text:
<p class="elem">text</p>
let elem = document.querySelector('.elem');
elem.textContent = '!!!';
The code execution result:
<p id="elem">!!!</p>
See also
-
the
getElementByIdmethod
that gets element by anid -
the
querySelectorAllmethod
that gets a group of elements by a selector