Get grandchild elements of document in javascript

user3340627 picture user3340627 · Mar 17, 2014 · Viewed 7.6k times · Source

How can I get grandchildren div and iframe of a document in javascript ? I have the id of div and name of iframe

Answer

Zach Saucier picture Zach Saucier · May 15, 2014

Use .querySelectorAll()

Example:

document.querySelectorAll("#iframeId .someSelector");

If you have an ID for child you can just use that:

document.getElementById("divId");

Or you could use a combination of selectors like so:

document.querySelectorAll("#iframeId > .someSelector > *");