Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can you search a HTMLElement for a particular class?

Say I have a DOM model that has a collection of elements with class: class_1 and I use document.getElementsByClassName("class_1") to get an HTMLCollection of these elements.

If I take the first element of the collection is it possible to use the .getElementsByClassName("...") function again to get a secondary HTMLCollection of all the class_2 elements within the first element?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can, with querySelector/querySelectorAll : Like so:

let allDivs = document.querySelectorAll('div');
let justThat = allDivs.querySelector('.subItemClass'); // null if not found
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading