I have two frames and want to access an element in one frame from another:
Frame 1:
<div id='someId'>...</div>
Frame 2:
var div=document.getElementById('someId');
div.innerHTML='something';
This is somehow not functioning in Firefox so I want to be sure, can I access an element in another frame by its ID?
To access a variable in one frame from javascript in another frame, you need a reference to the variable's containing window object. The window object has a property named "top", which is a reference to the top-level window object. This corresponds to the frameset window.
A commonly-used alternative to document. getElementById is using a jQuery selector which you read about more here.
frames property returns an array of all frames inside the current window. A frame can then be accessed by using its index or by its name. var element = window. frames['frame1'].
The getElementById() is a DOM method used to return the element that has the ID attribute with the specified value. This is one of the most common methods in the HTML DOM and is used almost every time we want to manipulate an element on our document. This method returns null if no elements with the specified ID exists.
You can refer the other frame by using
window.frames["framename"]
and then you can refer the element in the DOM by using
window.frames["framename"].document.getElementById ( "yourelementid" );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With