Last updated: Mar 4, 2024
Reading timeยท3 min
To find elements by content:
document.querySelectorAll
method to select DOM elements by tag.for...of
loop to iterate over the collection.textContent
of the element matches the
expected content.Here is the HTML for the example in the article.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div>Bobby Hadz</div> <div>One two three</div> <div>Apple banana kiwi</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const text = 'Bobby Hadz'; const matches = []; for (const div of document.querySelectorAll('div')) { if (div.textContent.includes(text)) { matches.push(div); } } console.log(matches); // ๐๏ธ [div.box]
We used the document.querySelectorAll
method to get a NodeList
that contains all the DOM elements that have a tag of
div
.
The next step is to use the
for...of loop to iterate over
the NodeList
.
On each iteration, we check if the element's
textContent includes the
expected text. If there is a match, we push the element into the matches
array.
If you want to ignore the case when checking if the DOM element's content contains the specific string, convert both strings to lowercase.
const text = 'BOBBY HADZ'; const matches = []; for (const div of document.querySelectorAll('div')) { if (div.textContent.toLowerCase().includes(text.toLowerCase())) { matches.push(div); } } console.log(matches); // ๐๏ธ [div.box]
By converting the element's textContent
and the text we are looking for to
lowercase, we are able to do a case-insensitive check.
for...of
loop iterates over all of the elements in the NodeList
, this approach would be inefficient if you only need the first element that matches the condition.Array.find()
This is a four-step process:
document.querySelectorAll
method to select elements by tag.Array.from()
method.find()
method to iterate over the array.const text = 'Bobby Hadz'; const elements = Array.from(document.querySelectorAll('div')); const match = elements.find(el => { return el.textContent.includes(text); }); console.log(match); // ๐๏ธ div.box
The function we passed to the Array.find() method gets called for each element in the array until it returns a truthy value or exhausts the elements in the array.
find()
method short-circuits and returns the corresponding array element.If the condition is never met, the find
method returns undefined
.
If you want to do a
case-insensitive check for whether the element contains the specific string,
convert the element's textContent
and the string you are checking for to
lowercase.
const text = 'BOBBY HADZ'; const elements = Array.from(document.querySelectorAll('div')); const match = elements.find(el => { return el.textContent.toLowerCase().includes(text.toLowerCase()); }); console.log(match); // ๐๏ธ div.box
By converting both of the strings we are comparing to lowercase, we are able to do a case-insensitive comparison.
Array.filter()
You can also use the Array.filter()
method to find an element by its content.
const text = 'Bobby Hadz'; const matches = Array.from( document.querySelectorAll('div'), ).filter(element => element.textContent.includes(text)); console.log(matches); // ๐๏ธ [div.box] matches.forEach(match => { console.log(match.textContent); // ๐๏ธ Bobby Hadz });
We used the Array.from()
method to convert the NodeList to an array to be able
to call the Array.filter()
method.
The function we passed to the Array.filter() method gets called with each element in the array.
The filter()
method returns a new array that only contains the elements that
meet the condition.
On each iteration, we check if the current element's textContent
contains a
specific string.
The filter
method returns a new array containing only the elements whose text
content contains the specified string.
You can learn more about the related topics by checking out the following tutorials: