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

javascriptdocument.querySelector(selector);

Example

Let's get the paragraph #elem and change its text with textContent:

html<p id="elem">text</p>
javascriptlet elem = document.querySelector('#elem');
elem.textContent = '!!!';

The code execution result:

html<p id="elem">!!!</p>

Example

Let's get the paragraph with the class elem and change its text:

html<p class="elem">text</p>
javascriptlet elem = document.querySelector('.elem');
elem.textContent = '!!!';

The code execution result:

html<p id="elem">!!!</p>

See also