473,802 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing table cells by Row #, Col #

I want to be able to access cells within an HTML table, via computed row
number and column number.

I was considering using oTableID.childr en[] and oTR.children[] arrays.

My question is what browsers and browser versions support this?

If the children[] constructs are not fairly universally supported, then what
is a better approach to doing this?

- David

Jul 23 '05 #1
6 7103
"David D." <da************ *************** **@comcast.net> wrote in message news:<AM******* *************@c omcast.com>...
I want to be able to access cells within an HTML table, via computed row
number and column number.

I was considering using oTableID.childr en[] and oTR.children[] arrays.

My question is what browsers and browser versions support this?

If the children[] constructs are not fairly universally supported, then what
is a better approach to doing this?

- David

What I've done may not be the prettiest solution, but it works. Each
cell in the table gets assigned an ID (<TD id="R5C6">xxxxx </TD>)

Then, if a user clicks in a cell, I use event.srcElemen t.id to get
the ID of the clicked cell, and take it from there.
Jul 23 '05 #2
Bruce,

Thanks for the suggestion.

Your technique is good for finding which cell was clicked on.

Now, suppose you wanted to change some attributes (e.g., the background
color) of each cell ADJACENT to the cell that was clicked on (above, below,
right, left and diagonal). That's why I was considering an indexed array
solution such as table.children[] and tr.children[]. Alternatively, using
your suggestion, I could parse the name and use the "eval" statement to
reference other cells. I just thought that an integer array index solution
would be easier and more efficient.

However, an important factor is my choice of solution is the percentage
of installed browsers that would support a particular solution.

- David
"bruce" <br************ *@glic.com> wrote in message
news:d3******** *************** ***@posting.goo gle.com...
"David D." <da************ *************** **@comcast.net> wrote in message

news:<AM******* *************@c omcast.com>...
I want to be able to access cells within an HTML table, via computed row
number and column number.

I was considering using oTableID.childr en[] and oTR.children[] arrays.

My question is what browsers and browser versions support this?

If the children[] constructs are not fairly universally supported, then what is a better approach to doing this?

- David

What I've done may not be the prettiest solution, but it works. Each
cell in the table gets assigned an ID (<TD id="R5C6">xxxxx </TD>)

Then, if a user clicks in a cell, I use event.srcElemen t.id to get
the ID of the clicked cell, and take it from there.

Jul 23 '05 #3
David D. wrote:
I want to be able to access cells within an HTML table, via computed
row number and column number.

I was considering using oTableID.childr en[] and oTR.children[]
arrays.
The - children - collection is a Microsoft extension that has also been
implemented by Opera, Konqueror, Safari and IceBrowser but has not been
copied by Mozilla or any other Gecko browser. The nearest direct W3C DOM
equivalent is the - childNodes - collection, but that is a collection of
Nodes and so it also includes non-element nodes and needs to be
traversed with an eye to the - nodeType - property of the objects found
(node.nodeType == 1 for an Element Node). While - children - is a
collection of exclusively element Nodes on IE and most other
implementing browsers (IceBrowser makes an odd decision here and
implements its - children - collection as a reference to its -
childNodes - collection, so it does contain non-element Nodes).
My question is what browsers and browser versions support this?
IE 4+, Opera 5+, Konqueror 3+ and Safari 1+ (maybe others), but not
Mozilla or any other Gecko browsers, and IceBrowser gets it wrong (at
least in version 5, version 6 has just been released and I haven't had a
chance to check it out yet).
If the children[] constructs are not fairly universally supported,
then what is a better approach to doing this?


The W3C HTML DOM specification adopted the - rows - and - cells -
properties that were introduced by Microsoft in IE 4. TABLE, THEAD,
TBODY and TFOOT elements have - rows - collections that hold references
to all the TR elements they contain. TR elements have - cells -
collections that refer to all the TD and TH elements they contain, in
order.

Those collections would be your best entry point while attempting to
refer to cells by index as they are (more or less) implemented on all
W3C HTML DOM implementing browser and IE 4+.

However, if a browser is tested and found not to implement a - rows -
collection on a, say, TABLE elements there are a number of possible
fall-back options that would give similar results:-

var tableRef = ... //assign a reference to a table element.
var rowsCollection;
if(tableRef.row s){
rowsCollection = tableRef.rows;
}else if(tableRef.get ElementsByTagNa me){
rowsCollection = tableRef.getEle mentsByTagName( 'TR');
}else if((tableRef.al l)&&(tableRef.a ll.tags)){
rowsCollection = tableRef.all.ta gs('TR');
}
if(rowsCollecti on){
/* We have an object, that may actually be the table's - rows -
collection (or an alternative), and it can give access to a
TR element by index.

A similar, but potentially more involved, strategy can now be
applied to that row to get to it's contained cells. More
involved because rows may contain both TD and TH cells (so -
getElementsByTa gName - and - tableRef.all.ta gs - are not so
easy to apply as fall-back).
*/
}

Richard.
Jul 23 '05 #4
David D. wrote:
<snip>
... indexed array solution such as table.children[] and
Strictly - table.children[] - would not contain references to TR
elements as TABLE elements are only allowed to directly contain CAPTION,
COL, COLGROUP, THEAD, TFOOT and TBODY elements. It is the THEAD, TFOOT
and TBODY elements that are the direct ancestors of TH and TD elements.
And each table will contain at least one TBODY, even if no tags for the
element are provided in the HTML as TBODY opening and closing tags are
optional. A TR placed directly within a table would always imply its
containing TBODY.

<snip> ... and use the "eval" statement to reference other cells. ...> <snip>

The - eval - function is never needed to access elements (even
dynamically), see:-

<URL: http://jibbering.com/faq/#FAQ4_39 >

- and its referenced article, and:-

<URL: http://jibbering.com/faq/#FAQ4_40 >
"bruce" <br************ *@glic.com> wrote in message


Please do not top-post to comp.lang.javas cript.

Richard.
Jul 23 '05 #5
Richard,

Thanks for your detailed answer. This sounds like the best approach
for me to take.

Actually, I just got back from a book store where I took a look at the
latest O'Reilley DHTML book. It said that cells[] and rows[] are supported
by IE 4 and NN 6. I do not know what percentage of IE and NN browsers are
older than that. But it sounds like your approach should cover most
browsers.

I will also take a look at the links from your other posting.

Thanks.

- David

"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message
news:c9******** ***********@new s.demon.co.uk.. .
David D. wrote:
I want to be able to access cells within an HTML table, via computed
row number and column number.

I was considering using oTableID.childr en[] and oTR.children[]
arrays.


The - children - collection is a Microsoft extension that has also been
implemented by Opera, Konqueror, Safari and IceBrowser but has not been
copied by Mozilla or any other Gecko browser. The nearest direct W3C DOM
equivalent is the - childNodes - collection, but that is a collection of
Nodes and so it also includes non-element nodes and needs to be
traversed with an eye to the - nodeType - property of the objects found
(node.nodeType == 1 for an Element Node). While - children - is a
collection of exclusively element Nodes on IE and most other
implementing browsers (IceBrowser makes an odd decision here and
implements its - children - collection as a reference to its -
childNodes - collection, so it does contain non-element Nodes).
My question is what browsers and browser versions support this?


IE 4+, Opera 5+, Konqueror 3+ and Safari 1+ (maybe others), but not
Mozilla or any other Gecko browsers, and IceBrowser gets it wrong (at
least in version 5, version 6 has just been released and I haven't had a
chance to check it out yet).
If the children[] constructs are not fairly universally supported,
then what is a better approach to doing this?


The W3C HTML DOM specification adopted the - rows - and - cells -
properties that were introduced by Microsoft in IE 4. TABLE, THEAD,
TBODY and TFOOT elements have - rows - collections that hold references
to all the TR elements they contain. TR elements have - cells -
collections that refer to all the TD and TH elements they contain, in
order.

Those collections would be your best entry point while attempting to
refer to cells by index as they are (more or less) implemented on all
W3C HTML DOM implementing browser and IE 4+.

However, if a browser is tested and found not to implement a - rows -
collection on a, say, TABLE elements there are a number of possible
fall-back options that would give similar results:-

var tableRef = ... //assign a reference to a table element.
var rowsCollection;
if(tableRef.row s){
rowsCollection = tableRef.rows;
}else if(tableRef.get ElementsByTagNa me){
rowsCollection = tableRef.getEle mentsByTagName( 'TR');
}else if((tableRef.al l)&&(tableRef.a ll.tags)){
rowsCollection = tableRef.all.ta gs('TR');
}
if(rowsCollecti on){
/* We have an object, that may actually be the table's - rows -
collection (or an alternative), and it can give access to a
TR element by index.

A similar, but potentially more involved, strategy can now be
applied to that row to get to it's contained cells. More
involved because rows may contain both TD and TH cells (so -
getElementsByTa gName - and - tableRef.all.ta gs - are not so
easy to apply as fall-back).
*/
}

Richard.

Jul 23 '05 #6
Richard Cornford wrote:
<snip>
... . It is the THEAD, TFOOT and TBODY elements that
are the direct ancestors of TH and TD elements. ...

<snip>

Obviously that should have read: " ... are the direct ancestors of TR
elements.".

Richard.
Jul 23 '05 #7

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

Similar topics

2
6666
by: Chris New | last post by:
G'Day All I am having trouble dynamically assigning a value to a table's cell from one frame to another frame. I've created a website that is primarily viewed in a frameset consisting of 2 frames - a navigation frame (navFrame) at the top and a contents frame (mainFrame) on the bottom. In all instances, the mainFrame displays a single page except in one case, where it was necessary to use a frameset consisting of a left frame...
15
2102
by: Christopher Benson-Manica | last post by:
When are named elements written with script accessible to script? <html><head><script type="text/javascript"> function ready() { alert( document.getElementsByName("div").length ); } </script></head> <body onload="ready()"> <script type="text/javascript"> document.open();
4
3310
by: jeffsal | last post by:
I am using sorttable.js to sort a table which works fine which allows a user to sort the table by clicking on the column header. Is there some code I could add to the page (onload or something) to auto sort by the first column without user clicking on it. Here is the sorttable.js code. addEvent(window, "load", sortables_init); var SORT_COLUMN_INDEX;
1
2161
by: Stephen | last post by:
I have got the below asp table and I would like to be able to call up the lblAddress1 label with in the asp table in the code behind page. I tried doing this.lblAddress1 and it didn't work and I tried doing this.tblSearchAddresses.lblAddress1 and it didn't work. Has anyone an idea how I call this lable with a asp table. Thanks in advance for any help anyone can give me. <asp:table id="tblSearchAddresses" Runat="server" Width="100%">...
2
2794
by: Bass Pro | last post by:
How do I access a textbox when it was added to a table cell? I've tried using the findcontrol function with no result. I create it as such... Dim myTable As Table = New Table Dim Row as TableRow row = New TableRowI Answer_1 = New TextBox Answer_1.Width = New Unit("100px")
7
4829
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
2
2455
by: ortega23 | last post by:
Hi, I am trying to obtain the column number of the parent of a cell, if the cell is a linked cell (i.e. its value depends on another cells value). I try to read "Precedents"(parent cells) of a "Range"(selection of a single cell in this case) for each cell in my table to access parents of the linked cells.
6
7998
by: Romulo NF | last post by:
Greetings again to everyone, Im back to show this grid componenet i´ve developed. With this grid you can show the data like a normal table, remove the rows that you need, add rows, import data, call determined functions, and edit the data already present What will you need to use the grid? A table with standard markup and an ID to call the script that will turn the table into a grid Parameters: tabelaID => id of the table that will...
5
4961
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.