Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript document.getElementById in other frames

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?

like image 263
ante.sabo Avatar asked Nov 27 '09 09:11

ante.sabo


People also ask

How can a script in one frame access another frame in Javascript?

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.

What can I use instead of document getElementById?

A commonly-used alternative to document. getElementById is using a jQuery selector which you read about more here.

How do you access an element in a frame?

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'].

Can we use document getElementById in JS?

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.


1 Answers

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" );
like image 163
rahul Avatar answered Oct 13 '22 12:10

rahul