How to pick element inside iframe using document.getElementById

JavascriptHtmlIframeGetelementbyidFrameset

Javascript Problem Overview


I have a iframe like this

<iframe name="myframe1" id="myframe1" width="100%" height="100%" src="a.html">
<html>
	<head></head>
	<frameset name="myframe2" cols="0%, 100%" border="0" frameBorder="0" frameSpacing="0">
		<frame name="page1" src="c.html" scrolling="no"></frame>
		<frame name="page2" src="d.html" >
			<html>
				<head></head>
				<body id="top">
					<div id="div1">
						<div id="div2">
							<div id="div3">
								<ul id="x">
									<li>a</li>
									<li>b</li>
								</ul>
							</div>
						</div>
					</div>
				</body>
			</html>
		
		</frame>
		
	</frameset>
</html>
</iframe>

I want to refer to the element "x". I tried in several ways but I couldn't find a solution.

Javascript Solutions


Solution 1 - Javascript

document.getElementById('myframe1').contentWindow.document.getElementById('x')

Fiddle

contentWindow is supported by all browsers including the older versions of IE.

Note that if the iframe's src is from another domain, you won't be able to access its content due to the Same Origin Policy.

Solution 2 - Javascript

use contentDocument to achieve this

var iframe = document.getElementById('iframeId');
var innerDoc = (iframe.contentDocument) 
               ? iframe.contentDocument 
               : iframe.contentWindow.document;

var ulObj = innerDoc.getElementById("ID_TO_SEARCH");

Solution 3 - Javascript

(this is to add to the chosen answer)

Make sure the iframe is loaded before you

contentWindow.document

Otherwise, your getElementById will be null.

PS: Can't comment, still low reputation to comment, but this is a follow-up on the chosen answer as I've spent some good debugging time trying to figure out I should force the iframe load before selecting the inner-iframe element.

Solution 4 - Javascript

You need to make sure the frame is fully loaded the best way to do it is to use onload:

<iframe id="nesgt" src="" onload="custom()"></iframe>

function custom(){
    document.getElementById("nesgt").contentWindow.document;
	}

this function will run automatically when the iframe is fully loaded.

Solution 5 - Javascript

In my case I was trying to grab pdfTron toolbar, but unfortunately its ID changes every-time you refresh the page.

So, I ended up grabbing it by doing so.

const pdfToolbar = document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('HeaderItems');

As in the array written by tagName you will always have the fixed index for iFrames in your application.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionishkView Question on Stackoverflow
Solution 1 - JavascriptFabrício MattéView Answer on Stackoverflow
Solution 2 - JavascriptHaryView Answer on Stackoverflow
Solution 3 - JavascriptPatView Answer on Stackoverflow
Solution 4 - JavascriptMostafa EsmaeiliView Answer on Stackoverflow
Solution 5 - JavascriptDivyanshu RawatView Answer on Stackoverflow