2

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

user3340627
  • 3,023
  • 6
  • 35
  • 80
  • get direct grandchild by `document.getElementById('someid')` if your grandchild has id. – Suman Bogati Mar 17 '14 at 15:36
  • You can use the `parentNode` of the `parentNode` like this: `document.getElementById('someid').parentNode.parentNode` – Ex-iT Mar 17 '14 at 15:39
  • Well, my problem is that although my "div" and "Iframe" have ids, but when I try to retrieve them using document.getElementById I get the error "object is null" so it looks like it can't see it. I need a way to navigate through the children and grandchildren, is there a way to do that ? – user3340627 Mar 19 '14 at 08:15
  • @user3340627 Any feedback for me? – Zach Saucier Jun 11 '14 at 12:22
  • @ZachSaucier Sorry for my late reply, but I'm unable to test at the moment, as I've moved on in the project. I will give it a try as soon as I get a chance. – user3340627 Jun 12 '14 at 11:04

1 Answers1

5

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 > *");
Zach Saucier
  • 24,871
  • 12
  • 85
  • 147