easiest way to find child-tags with certain name

A

arne

Hi

what would be the easiest way to find all tag's with a certain name (or
class) below (within) a certain tag.
Pref jscript DOM

thx
Arne
 
G

Gérard Talbot

arne a écrit :
Hi

what would be the easiest way to find all tag's with a certain name (or
class) below (within) a certain tag.
Pref jscript DOM

thx
Arne

For links: document.links
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-7068919

For images: document.images
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-90379117

For forms: document.forms
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-1689064

For applets: document.applets
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-85113862

For anchors: document.anchors
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-7577272

All these return an HTML collection.

For other elements (e.g. <p>): document.getElementsByTagName("p")
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-A6C9094
This returns a nodelist.

There are other lists possible. Like rows, cells.

var rowCollection = document.getElementById("idTable").rows;
will return a list of row nodes.
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-6156016

var cellCollection = rowCollection[3].cells;
will return a list of cell nodes from within the 4th row.
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-67349879

There is also the options list:
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-30606413

Within a node, you can do:

document.getElementById("section").getElementsByTagName("p")
which would return a list of <p> nodes from within a div identified as
"section".

Gérard
 
R

RobG

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.

Pref jscript DOM

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top