473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

easiest way to find child-tags with certain name

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
2 20219
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eshrath | last post by:
Hi, I have an xsl where a particular child node (<sub1> and <sub2> )can occur in two type of parent nodes (<A> and <B>). like <A> <sub1>11</sub1> <sub2>22</sub2>
7
by: Dan V. | last post by:
Situation: I have to connect with my Windows 2000 server using VS.NET 2003 and C# and connect to a remote Linux server at another company's office and query their XML file. Their file may be...
4
by: Captain Wonky | last post by:
As the subject says... I'm a database novice even though I've been trying to learn Access for years. I've 'almost finished' several databases but always get stumped on something - this time it's...
0
by: John Smith | last post by:
I have an asp.net application in which I have a child assembly that references a parent assembly. Using reflection, in the parent I need to create an instance of a class in the child assembly. In...
6
by: jan | last post by:
My apologies for being a javascript beginner and asking such a basic question. This is probably so easy that nobody ever mentions it. Tutorials and places that tell of basic commands never seem to...
4
by: Mike Charney | last post by:
What is the easiest and simplest way to find out if a table exists in an Access Data Project? (SQL Svr 2000 Standard using MS-Access 2003 for connection.) Mike Charney m charney at dunlap...
7
by: john | last post by:
In my form I have a master table and a details table linked 1xM. I can search through the whole parent table but I also like to be able to search through the child table fields to find parent...
5
by: gnewsgroup | last post by:
In my user control, I would like to find a Label control in the parent page (the page that uses my user control). I need to update that Label.Text when something happens in the user control. I...
2
by: shapper | last post by:
Hello, I have a control named Parent in my page. Parent has many child controls under it which also have other child controls under them. I need to find a control named "A" which i don't know...
4
by: Mudcat | last post by:
So I haven't programmed much in Python the past couple of years and have been catching up the last few days by reading the boards. I'll be making commercial Python applications again and wanted to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.