How to get all direct descendants that match a test condition with vanilla JS

Let’s say you have some HTML like this.
<div id=”sandwiches”> <div id=”one” class=”tuna”>This will match</div> <div> Hello, world! <div id=”two” class=”tuna”>This will not</div> </div> <div id=”three” class=”tuna”>So will this</div> </div> You want to get only direct descendants of the #sandwich element that have the .tuna class: #one and #three. The #two element also has the .tuna class but is nested inside another element, so we don’t want that one.
You can use the Element.


This content originally appeared on Go Make Things and was authored by Go Make Things

Let’s say you have some HTML like this.

<div id="sandwiches">
	<div id="one" class="tuna">This will match</div>
	<div>
		Hello, world!

		<div id="two" class="tuna">This will not</div>
	</div>
	<div id="three" class="tuna">So will this</div>
</div>

You want to get only direct descendants of the #sandwich element that have the .tuna class: #one and #three. The #two element also has the .tuna class but is nested inside another element, so we don’t want that one.

You can use the Element.querySelectorAll() method to get all elements by a certain selector inside another element, but it will search the entire tree in that element and return #one, #two, and #three.

let sandwiches = document.querySelector('#sandwiches');

// Not what we want
let tuna = sandwiches.querySelectorAll('.tuna');

We could also try using the Node.children property, but that returns all direct descendants, regardless of class.

// Not what we want, either
let tuna = sandwiches.children;

To do this, we need to combine Node.children with a few other techniques.

We can use Array.from() to turn the NodeList that Node.children returns into an array, and then use the Array.filter() method to remove an elements that don’t match a test we specify.

// This WILL work
let tuna = Array.from(sandwiches.children).filter(function (elem) {
	return elem.matches('.tuna');
});

Here’s a demo.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-04-20T14:30:00+00:00) How to get all direct descendants that match a test condition with vanilla JS. Retrieved from https://www.scien.cx/2021/04/20/how-to-get-all-direct-descendants-that-match-a-test-condition-with-vanilla-js/

MLA
" » How to get all direct descendants that match a test condition with vanilla JS." Go Make Things | Sciencx - Tuesday April 20, 2021, https://www.scien.cx/2021/04/20/how-to-get-all-direct-descendants-that-match-a-test-condition-with-vanilla-js/
HARVARD
Go Make Things | Sciencx Tuesday April 20, 2021 » How to get all direct descendants that match a test condition with vanilla JS., viewed ,<https://www.scien.cx/2021/04/20/how-to-get-all-direct-descendants-that-match-a-test-condition-with-vanilla-js/>
VANCOUVER
Go Make Things | Sciencx - » How to get all direct descendants that match a test condition with vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/20/how-to-get-all-direct-descendants-that-match-a-test-condition-with-vanilla-js/
CHICAGO
" » How to get all direct descendants that match a test condition with vanilla JS." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/04/20/how-to-get-all-direct-descendants-that-match-a-test-condition-with-vanilla-js/
IEEE
" » How to get all direct descendants that match a test condition with vanilla JS." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/04/20/how-to-get-all-direct-descendants-that-match-a-test-condition-with-vanilla-js/. [Accessed: ]
rf:citation
» How to get all direct descendants that match a test condition with vanilla JS | Go Make Things | Sciencx | https://www.scien.cx/2021/04/20/how-to-get-all-direct-descendants-that-match-a-test-condition-with-vanilla-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.