Zero4 Zero4 - 5 months ago 15
HTML Question

javascript - access iframe's elements

I need to change the style of my frame element, however the iframe has no id or class or name

<div id="A">
<iframe src="iframe-left.html">
<div class="login" style="display : none;"> </div>
</iframe>
</div>
<div id="B">
<iframe src="iframe-right.html">
<button onclick = "execute()">Submit</button>
</iframe>
</div>

function execute()
{
parent.document.getElementsByTagName("iframe").contentDocument.querySelector(".login").style.display = 'block';
}


What's the mistake in the javascript function?

Answer

Hi I think you have to get the first element of the node list, see http://www.w3schools.com/jsref/met_document_getelementsbytagname.asp

In other words you have to do:

parent.document.getElementsByTagName("iframe")[0].contentDocument.querySelector(".login").style.display = 'none';