...

View Full Version : Help : getElementsByTagName



quocbao
05-30-2005, 05:24 PM
Hello , i've a small question .

does the function getElementsByTagName work correctly in IE , Molliza , FireFox , Netscape ?

brandonH
05-30-2005, 07:04 PM
do you understand what getElementByTagName does?

it doesnt actually grab an element by its name attribute.
it grabs the elements type.
example:

<A href=#stahere id=a1 name=a1></a>
it grabs " A "
<td id=td1 name=td1></td>
it grabs " td "


it does work in internet explorer and netscape. as far as I've heard it works in mozilla. I do not know about firefox.

ChiefWigs1982
05-31-2005, 12:33 AM
Yes, it works fine in all browsers. It returns an array of referencess to
all the elements in a page of a certain type.

For example,
var links=document.getElementsByTagName('a');will create an array with references to all the links within your page.

So if you want to do anything, you need to loop through for the length
of the array, and apply your changes to each item individually.


That said, what is it specifically you are having troubles with?

quocbao
05-31-2005, 06:58 AM
Thanks all ! :)

Because i will use this function in my script , and i want it work correctly in every browsers .

Thansk (again :) )

quocbao
05-31-2005, 09:36 AM
Hi , i've written a function to get all child elements by tag name .



function ElementByTag(tag , bdocument){
if (!bdocument) bdocument = document;
elements = bdocument.getElementsByTagName(tag);
return_elements = new Array;
i = 0;
while (true) {
if (!elements[i]) break;
return_elements[i] = elements[i];
i++;
}
return return_elements;
}


It works correctly in Opera , NetScape , IE and FireFox .

But i don't know if it will work in Molliza or other browsers and is there some mistakes in the function ?

Can someone help me ?

glenngv
05-31-2005, 11:18 AM
If it works with Firefox, it is assumed to work with other Gecko-based browsers as they have the same codebase.

Your code is correct but you should declare the variables with var keyword to make them local.

function ElementByTag(tag , bdocument){
if (!bdocument) bdocument = document;
var elements = bdocument.getElementsByTagName(tag);
var return_elements = new Array();
var i = 0;
while (elements[i]) {
return_elements[i] = elements[i];
i++;
}
return return_elements;
}
The while-loop can also be changed to for-loop.

function ElementByTag(tag , bdocument){
if (!bdocument) bdocument = document;
var elements = bdocument.getElementsByTagName(tag);
var return_elements = new Array();
var len = elements.length;
for (var i=0; i<len; i++) {
return_elements[i] = elements[i];
}
return return_elements;
}

quocbao
05-31-2005, 04:38 PM
thanks a lot :)

SpirtOfGrandeur
05-31-2005, 04:54 PM
*edited till i study opera a bit more*

quocbao
05-31-2005, 04:59 PM
Because : The return value of this function in Opera is different from IE , and also other browsers .

I want to have a function which return only the child elements , no misc data or information (to use in for ... in



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum