[JS] How do I get element from within two iframes

I’m trying to run code on a page but the element it is searching for is inside of an iframe inside of an iframe and I don’t know how to get this to work without inspecting the element the second iframe is in. Below is the sample code:

var searchClass=document.getElementsByClassName("w3-bar w3-card-2 notranslate");

Easiest example of exactly what I’m talking about is if you go to this example site , there is the iframe “myFrame” inside of the iframe “iframeResult”.

My question is using that example site as a reference, how would you search for a class inside of two different iframes (assuming they’re from the same domain)?. I found the code below as an example but it would not open even the first one and I honestly don’t know why:

var iframe = document.getElementById('iframeResult');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

This is not GitHub-related, but the solution is to run getElementById again on innerDoc.

1 Like