251

What would the most efficient method be to find a child element of (with class or ID) of a particular parent element using pure javascript only. No jQuery or other frameworks.

In this case, I would need to find child1 or child2 of parent, assuming that the DOM tree could have multiple child1 or child2 class elements in the tree. I only want the elements of parent

<div class="parent">
    <div class="child1">
        <div class="child2">
        </div>
    </div>
</div>
Asef Hossini
  • 655
  • 8
  • 11
Blyde
  • 2,923
  • 2
  • 18
  • 16

5 Answers5

345

If you already have var parent = document.querySelector('.parent'); you can do this to scope the search to parent's children:

parent.querySelector('.child')
csch
  • 4,496
  • 2
  • 19
  • 23
234

The children property returns an array of elements, like so:

parent = document.querySelector('.parent');
children = parent.children; // [<div class="child1">]

There are alternatives to querySelector, like document.getElementsByClassName('parent')[0] if you so desire.


Edit: Now that I think about it, you could just use querySelectorAll to get decendents of parent having a class name of child1:

children = document.querySelectorAll('.parent .child1');

The difference between qS and qSA is that the latter returns all elements matching the selector, while the former only returns the first such element.

jmargolisvt
  • 5,722
  • 4
  • 29
  • 46
Rick Viscomi
  • 8,180
  • 4
  • 35
  • 50
  • I know this answer is 10+ years old, but because it might be still found on Google... Technically, `children` doesn't return an `array`, but an `HTMLCollection`. As in `Array.isArray(document.children) == false`. You need to use `Array.from(document.children)` to convert it to a proper array. – Jorjon Jun 15 '23 at 19:13
28

Just adding another idea you could use a child selector to get immediate children

document.querySelectorAll(".parent > .child1");

should return all the immediate children with class .child1

Waleed
  • 349
  • 3
  • 6
  • I've been seeing everyone suggest querySelector than getElementBy**. Is it more efficient? – Andre Jul 22 '19 at 17:57
  • Maybe this will help. https://stackoverflow.com/questions/14377590/queryselector-and-queryselectorall-vs-getelementsbyclassname-and-getelementbyid – Waleed Jul 26 '19 at 02:44
  • This will not return `.child2`, and other children, as asked by @Blyde – Esger Jan 14 '21 at 08:23
18

Element.querySelector() is the best way;

const parent = document.querySelector('.parent'); 
// Do anything next //When you want
parent.querySelector('.child1'); // <div class="child1">
jacobkim
  • 1,043
  • 10
  • 20
FATCHOLA
  • 393
  • 2
  • 4
  • When I use query selector against a parent that has three img elements that all have .img-fluid only the first img element is returned. How can I select all ? – b_dubb Oct 12 '22 at 20:16
  • 2
    @b_dubb you can use querySelectorAll instead to get all elements – Mhd Saleh Jan 12 '23 at 07:33
3

You have a parent element, you want to get all child of specific attribute 1. get the parent 2. get the parent nodename by using parent.nodeName.toLowerCase() convert the nodename to lower case e.g DIV will be div 3. for further specific purpose, get an attribute of the parent e.g parent.getAttribute("id"). this will give you id of the parent 4. Then use document.QuerySelectorAll(paret.nodeName.toLowerCase()+"#"_parent.getAttribute("id")+" input " ); if you want input children of the parent node

let parent = document.querySelector("div.classnameofthediv")
let parent_node = parent.nodeName.toLowerCase()
let parent_clas_arr = parent.getAttribute("class").split(" ");
let parent_clas_str = '';
  parent_clas_arr.forEach(e=>{
     parent_clas_str +=e+'.';
  })
let parent_class_name = parent_clas_str.substr(0, parent_clas_str.length-1)  //remove the last dot
let allchild = document.querySelectorAll(parent_node+"."+parent_class_name+" input")
Krystian G
  • 2,842
  • 3
  • 11
  • 25
Adio Azeez
  • 31
  • 1