arne said:
Hi
what would be the easiest way to find all tag's with a certain name (or
Presumably you mean tag name, since the name attribute is not allowed
on many tags.
The scope of getElementsByTagName is the element you use it with. To
get, say, all the P elements in a document:
var allThePs = document.getElementsByTagName('p');
To get all the P elements inside a div with id="aDiv":
var aDiv = document.getElementById('aDiv');
var somePs = aDiv.getElementsByTagName('p');
class) below (within) a certain tag.
To get all the elements with a particular className inside aDiv:
if (aDiv.getElementsByTagName){
var aDivKids = aDiv.getElementsByTagName('*');
var someClassArray = [];
var x;
for (var i=0, j=aDivKids.length; i<j; ++i){
x = aDivKids
;
if (x.className && 'aClassName' == x.className){
someClassArray.push(x);
}
}
The className method is not recommended at all. Apart from being
slow, getElementsByTagName('*') is not supported in all browsers, you
will have to use document.all for older versions of IE.
So something like (based on a Mike Winter post[1]):
/* If the previous attempt failed... */
if(!aDivKids || !aDivKids.length) {
/* Check to see if we can use the all property
* to obtain a collection of all elements in
* the document.
*
* Note that this is *not* a check for IE, but
* a feature test.
*/
if( document.all ) {
aDivKids = document.all ;
/* If not, return an empty array as failure. */
} else {
aDivKids = [];
}
}
// Now do the class-hunting bit...
for (var i=0, j=aDivKids.length; i<j; ++i){
x = aDivKids;
if (x.className && 'aClassName' == x.className){
someClassArray.push(x);
}
}
The fall back to document.all will get every element in the document,
not just the decedents of aDiv.
getElementById and getElementsByTagName are W3C DOM, document.all is
MS (and supported in places by other browsers).
1.
<URL:http://groups.google.com.au/group/c...Name('*')+document.all&hl=en#0db5d830a30537ff>