Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementById() for children of an element

Tags:

javascript

getElementById() is on the document object.

If I have an element reference already, how can I find the child (or children's child etc) that has a given ID?

var parent = document.getElementById(parentId),
    child = parent.getElementById(childId);

This code fails because normal elements on a page do not have a getElementById method.

(I'm in maintenance mode, IDs are not necessarily unique, and cannot use jQuery).

like image 285
Will Avatar asked Mar 07 '13 10:03

Will


1 Answers

You could use querySelector():

child = parent.querySelector("#" + childId);
like image 80
nnnnnn Avatar answered Sep 17 '22 14:09

nnnnnn