By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,817 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,817 IT Pros & Developers. It's quick & easy.

easiest way to find child-tags with certain name

P: n/a
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

Oct 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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-HTM...tml#ID-7068919

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

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

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

For anchors: document.anchors
http://www.w3.org/TR/DOM-Level-2-HTM...tml#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-Le...tml#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-HTM...tml#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-HTM...ml#ID-67349879

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

Within a node, you can do:

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

Gérard
--
remove blah to email me
Oct 13 '05 #2

P: n/a
arne wrote:
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[i];
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[i];
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/comp.lang.javascript/browse_frm/thread/4dc9008e52064056/0db5d830a30537ff?tvc=1&q=getElementsByTagName(%27* %27)+document.all&hl=en#0db5d830a30537ff>

--
Rob
Oct 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.