offline version v3


145 of 264 menu

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

enru