473,396 Members | 2,011 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,396 software developers and data experts.

Quick way to find all tds?



I'm relatively new to JS so this question is very naive. Is there
a way to quickly get an array of all the HTML elements of a given
class, say all the <td ...>, or all the <font ...> elements? The
only way I can think of involves a tedious tree traversal.

Thanks!

bill
Jul 20 '05 #1
3 1382
On Thu, 12 Feb 2004 03:29:39 +0000 (UTC), bill <bi**********@yahoo.com>
wrote:
I'm relatively new to JS so this question is very naive. Is there
a way to quickly get an array of all the HTML elements of a given
class, say all the <td ...>, or all the <font ...> elements? The
only way I can think of involves a tedious tree traversal.


document.getElementsByTagName(), a DOM method can be used to retrieve
elements by element name. The DHTML equivalent is document.all.tags().
Note that both can be used with any HTML object (such as DIV, TABLE, BODY,
and FORM references) so the browser won't have to traverse the entire tree.

Before using either, you should test for their support:

var tableCells = null;

if( document.getElementsByTagName ) {
tableCells = document.getElementsByTagName('td');
} else if( document.all && document.all.tags ) {
tableCells = document.all.tags('td');
}
if( tableCells ) {
// tableCells is now a collection that contains
// all table cell elements in the document.
}

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2

That's great. Thanks!

bill

Jul 20 '05 #3
On Thu, 12 Feb 2004 09:39:38 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:

[snip]
document.getElementsByTagName(), a DOM method can be used to retrieve
elements by element name. The DHTML equivalent is document.all.tags().
Note that both can be used with any HTML object (such as DIV, TABLE,
BODY, and FORM references) so the browser won't have to traverse the
entire tree.


[snip]

After re-reading that last sentence, I came to the conclusion that it's
rather lacking in detail. What I meant was that getElementsByTagName() is
not just a method of the document object, but of all HTML elements. This
allows you to get all of the table cells of a specific table, for example.
You would first get a reference to the table (using getElementById() or
similar), then call getElementsByTagName() from that object.

Using an example similar to the last one:

var tableRef = null;

if( document.getElementById ) {
tableRef = document.getElementById('myTable');
} else if( document.all ) {
tableRef = document.all['myTable'];
}
if( tableRef ) {
var tableCells = null;

if( tableRef.getElementsByTagName ) {
tableCells = tableRef.getElementsByTagName('td');
} else if( tableRef.all && tableRef.all.tags ) {
tableCells = tableRef.all.tags('td');
}
if( tableCells ) {
// tableCells is now a collection that contains
// all table cell elements in the table, myTable
// (referenced by tableRef).
}
}

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: aredo3604gif | last post by:
The user can dynamically enter and change the rule connection between objects. The rule is a "<" and so given two objects: a < b simply means that b < a can't be set, also it must be a != b. And...
1
by: aredo3604gif | last post by:
The user can dynamically enter and change the rule connection between objects. The rule is a "<" and so given two objects: a < b simply means that b < a can't be set, also it must be a != b. And...
0
by: aredo3604gif | last post by:
The user can dynamically enter and change the rule connection between objects. The rule is a "<" and so given two objects: a < b simply means that b < a can't be set, also it must be a != b. And...
1
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a <...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.