Click to See Complete Forum and Search --> : Search frame for an element


Manju123
10-15-2003, 01:52 PM
Hi Folks ,,,
I have got something interesting...
Can u guys tell me as to how can I write a javascript function that searches a frame for an element with a given id.....
The functions should return the number of elements found

Rgds,
:rolleyes:
Manju

sciguyryan
10-15-2003, 02:06 PM
try something like this:

var Find = window[number].document.getElementById("Name of element here")

Or

var Find = WindowName.document.getElementById("Name of element here")

Manju123
10-15-2003, 02:10 PM
Hi I need to write a javascript function that searcha frame for an element with a given Id, and recursively all the frams that it contains???
The text inside matching elements should be Uppercased. and the function should return the number of elements found????
could u guys give me a complete function call...

sciguyryan
10-15-2003, 02:14 PM
That will find the elements. If you use this:


parent.document.getElementById("Name of element here")


it will search the main window - you will have to have seperate functions for each or, possibly use a loop to go through each window.

Manju123
10-15-2003, 02:50 PM
HI Ryan,
can i do it this way??
<SCRIPT>
function fnGetId(){
// Returns the first DIV element in the collection.
var oVDiv1=document.getElementById("oDiv1");
var oVDiv2=document.getElementById("oDiv2");
var oVDiv3=document.getElementById("oDiv3");
}
</SCRIPT>
<DIV ID="oDiv1">Div #1</DIV>
<DIV ID="oDiv2">Div #2</DIV>
<DIV ID="oDiv3">Div #3</DIV>
<INPUT TYPE="button" VALUE="Get Names" onclick="fnGetId()">

sciguyryan
10-15-2003, 02:57 PM
yes, that would find the element - what is it you want to do with the information?

Manju123
10-15-2003, 03:09 PM
Hi ryan,,
Yeh! I would likne to know as how can i recursively search all the id's in the frames that it contains..??

Rgds,
Pradeep

sciguyryan
10-16-2003, 09:48 AM
That would be a bit more complicated - you would have to use a loop to search the elements.

Mr J
10-18-2003, 05:42 AM
Please try the following, amend it to point to your frame


<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>
<script language="javascript">
<!--
function showme(){
how_many = document.getElementsByTagName("DIV").length
for(i=0;i<=document.getElementsByTagName("DIV").length-1;i++){
my_name=document.getElementsByTagName("DIV")[i].id
my_contents=document.getElementsByTagName("DIV")[i].innerHTML
my_posx=document.getElementsByTagName("DIV")[i].offsetLeft
my_posy=document.getElementsByTagName("DIV")[i].offsetTop

alert("There are "+how_many+" Divs\n\n"
+"DIV"+(i+1)+" ID is "+my_name+"\n\n"
+"DIV"+(i+1)+" contents are "+my_contents+"\n\n"
+"DIV"+(i+1)+" position left is "+my_posx+" ; and top "+my_posy+"\n\n")
}


}
// -->
</script>

<a href="#null" onclick="showme()">Show Me</a>


<div id="odiv_1">1</div>
<div id="odiv_2">2</div>
<div id="odiv_3">3</div>
<div id="odiv_4">4</div>
<div id="odiv_5">5</div>

</BODY>
</HTML>

Manju123
10-18-2003, 08:57 AM
Hi J,
The code is prefect but I have another query crawling up.
How do I write a function that searches a frame for an element with a given id and recursively all the frames that it contains. .

The Function should return the number of elements found.

Rgds,
Man:( :(

Mr J
10-18-2003, 09:28 AM
So the element in a frame contains other frames?

Manju123
10-19-2003, 01:26 AM
HI J,
Yep..

Mr J
10-19-2003, 03:11 AM
Take a look in the attachment and open index.htm