0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>In frontend tasks, you usually manipulate a set of elements located deep down in the DOM tree. Moreover, these elements are often scattered over different parts of it.</p>
1
<p>In frontend tasks, you usually manipulate a set of elements located deep down in the DOM tree. Moreover, these elements are often scattered over different parts of it.</p>
2
<p>For example, we can mark a list of files for deletion and then perform deleting. If you look at it from the perspective of changing the DOM tree, this task boils down to select all the elements that represent files and then deleting them.</p>
2
<p>For example, we can mark a list of files for deletion and then perform deleting. If you look at it from the perspective of changing the DOM tree, this task boils down to select all the elements that represent files and then deleting them.</p>
3
<h2>Search methods</h2>
3
<h2>Search methods</h2>
4
<p>In such a case, manually traversing through the tree will be tedious. DOM offers several ways to solve this problem at once. The simplest way to search is to<strong>search by ID</strong>:</p>
4
<p>In such a case, manually traversing through the tree will be tedious. DOM offers several ways to solve this problem at once. The simplest way to search is to<strong>search by ID</strong>:</p>
5
<p>According to the specification, there cannot be two identical id on a page. So, the getElementById() method always returns one element.</p>
5
<p>According to the specification, there cannot be two identical id on a page. So, the getElementById() method always returns one element.</p>
6
<p>On the other hand, there is a chance there may be several tags with the same id in HTML. In this situation, the browser will return the first element it encounters.</p>
6
<p>On the other hand, there is a chance there may be several tags with the same id in HTML. In this situation, the browser will return the first element it encounters.</p>
7
<p>If you need to process several elements at once, then<strong>search by a class</strong>is a better option:</p>
7
<p>If you need to process several elements at once, then<strong>search by a class</strong>is a better option:</p>
8
<p>If necessary, you can<strong>search by tags</strong>. It is rare in practical tasks, but still, it's useful to know about this method:</p>
8
<p>If necessary, you can<strong>search by tags</strong>. It is rare in practical tasks, but still, it's useful to know about this method:</p>
9
<h2>Search by selector</h2>
9
<h2>Search by selector</h2>
10
<p>The most universal search method is a<strong>search by a selector</strong>. You may remember that a selector is a rule that allows you to describe a set of elements in a DOM tree:</p>
10
<p>The most universal search method is a<strong>search by a selector</strong>. You may remember that a selector is a rule that allows you to describe a set of elements in a DOM tree:</p>
11
<p>We can apply both querySelector() and querySelectorAll() methods to the entire document or a specific element. The search will also go through all the descendants, as usual.</p>
11
<p>We can apply both querySelector() and querySelectorAll() methods to the entire document or a specific element. The search will also go through all the descendants, as usual.</p>
12
<h2>Other useful methods</h2>
12
<h2>Other useful methods</h2>
13
<h3>matches</h3>
13
<h3>matches</h3>
14
<p>The predicate el.matches(css) checks whether el satisfies the css selector:</p>
14
<p>The predicate el.matches(css) checks whether el satisfies the css selector:</p>
15
<h3>closest</h3>
15
<h3>closest</h3>
16
<p>The el.closest(css) method searches for the closest element higher up the hierarchy that satisfies the selector. The element itself is also analyzed. If such an element is found, it will be returned, otherwise null will be returned:</p>
16
<p>The el.closest(css) method searches for the closest element higher up the hierarchy that satisfies the selector. The element itself is also analyzed. If such an element is found, it will be returned, otherwise null will be returned:</p>
17
<h2>XPath</h2>
17
<h2>XPath</h2>
18
<p>It is a query language, developed to navigate through DOM trees in XML. It is supported by browsers:</p>
18
<p>It is a query language, developed to navigate through DOM trees in XML. It is supported by browsers:</p>
19
<p>The XPath path /html/body/*/span/@class will correspond to two elements of the source document in it:</p>
19
<p>The XPath path /html/body/*/span/@class will correspond to two elements of the source document in it:</p>
20
<ul><li><span class="text"> the first block in the third layer</span></li>
20
<ul><li><span class="text"> the first block in the third layer</span></li>
21
<li><span class="text">the second block in the third layer</span></li>
21
<li><span class="text">the second block in the third layer</span></li>
22
</ul><p>In everyday work, programmers rarely use XPath when working with DOM, so we're only learning it here to show the whole picture. But if you're working with XML documents, XPath is the main way to navigate a document.</p>
22
</ul><p>In everyday work, programmers rarely use XPath when working with DOM, so we're only learning it here to show the whole picture. But if you're working with XML documents, XPath is the main way to navigate a document.</p>