473,472 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do you access rows and columns of a HTML table?

This example applies to javascript, table, cells, rows

How do you access rows and columns of a HTML table?

<script language="javascript">
alert('start');
var tabl = document.getElementById('ordersTable');
alert( tabl.rows.length);
var l = tabl.rows.length;
var i = 0;
var s = "";
for (i = 0; i < l; i++ )
{
var tr = tabl.rows(i);
alert(tr);
alert(tr.cells(0));
var cll = tr.cells(0);
alert(cll.innerText);
s = s + "|" + cll.innerText;
}
alert("result=" + s);
</script>

Jul 23 '05 #1
1 57445
an******@hotmail.com wrote:
This example applies to javascript, table, cells, rows
It is poorly written. Please indent using two or four spaces, it makes
quoted posts much better to read.
How do you access rows and columns of a HTML table?

<script language="javascript">


The language attribute is depreciated, type is required.

<script type="text/javascript">
alert('start');
var tabl = document.getElementById('ordersTable');
If the intention is to support older browsers, a document.all
alternative should also be provided - see the group FAQ:

<URL:http://www.jibbering.com/faq/#FAQ4_15>
alert( tabl.rows.length);
var l = tabl.rows.length;
Why get the length twice? And why not let the user know what the
alert is for? A better way would be:

var l = tabl.rows.length;
alert( 'Number of table rows: ' + l );
var i = 0;
i is given a value here and then again in the for statement. Why do it
twice? Better to delete the above declaration and put it in the for
statement.
var s = "";
for (i = 0; i < l; i++ )
for ( var i = 0; i < l; i++ )
{
var tr = tabl.rows(i);
The proper syntax for collections is to use square brackets - round
brackets are an IE-ism.

var tr = tabl.rows[i];
alert(tr);
alert( 'Found ' + tr.nodeName + ' ' + i );
alert(tr.cells(0));
var cll = tr.cells(0);
Two calls are made to the cell where one would do.

Not all browsers support accessing table cells through the cells
collection (Safari is one). It is likely better to use the childNodes
collection:

var cll = tr.childNodes[0];
alert( 'Found a ' + cll.nodeName );

This loop will only get the first cell in each row. Is that the
intention? The subject indicates access to the columns, not just the
first column.
alert(cll.innerText);
Again, two calls to the element when one would do.

innerText is supported by IE and very few other browsers. innerHTML
and a regular expression to strip out HTML tags would do a better job
(though innerHTML is not a standard). DOM 3's 'Node.textContent' will
provide a standards-compliant method of extracting the text within a
node that is similar to innerText, however DOM 3 is not yet widely
supported:

<URL:http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent>

var ct = cll.innerHTML.replace( /<[^<>]+>/g, '' );
alert( cll.nodeName + ' contains text ' + ct );

s = s + "|" + cll.innerText;
Why not:

s += "|" + cll.innerText;

}
alert("result=" + s);
</script>

Here's the full script:
<table id="ordersTable">
<tr><td> cell <b>0</b> </td><td> another cell </td></tr>
<tr><td> cell <b>1</b> </td><td> another cell </td></tr>
<tr><td> cell <b>2</b> </td><td> another cell </td></tr>
</table>
<script type="text/javascript">

alert('Starting...');
var tabl = document.getElementById('ordersTable');
var l = tabl.rows.length;
alert( 'Number of table rows: ' + l );
var s = '';
for ( var i = 0; i < l; i++ )
{
var tr = tabl.rows[i];
alert( 'Found ' + tr.nodeName + ' ' + i );
var cll = tr.childNodes[0];
alert( 'Found a ' + cll.nodeName );
var ct = cll.innerHTML.replace( /<[^<>]+>/g, '' );
alert( cll.nodeName + ' contains text: ' + ct );
s += '|' + cll.innerText;
}
alert('result:\n' + s);

</script>


--
Rob
Jul 23 '05 #2

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

Similar topics

11
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML....
19
by: Logix | last post by:
Hello! I'm trying to make a sort of online page building system. In order to do this, I represent my page using a HTML table. One of the most basic templates would be a page divided in six...
3
by: bubbles | last post by:
i have a HTML file and it already has a HTML table and a few rows created by default.. however, i would like to add more rows to this table programmatically may i know how?
1
by: Al Wilkerson | last post by:
Hey, I have a Web Form with a drop down list, textbox, and search button. When click the search button an SQL server database is queried fordata. Once I have the data in a dataset I use the...
0
by: Subba Rao via DotNetMonster.com | last post by:
---------------------------HTML---------------------------------------- <html> <head> <title>:: DHTML Table Demo ::</title> <script langauge="javascript" src="InterchangeRows.js"></script>...
2
by: Sehboo | last post by:
I have an ASP.NET page which has bunch of stuff. In the middle somewhere (in a table), I want to add rows of records from database. So when user clicks on this button, I want to go read the data...
2
by: alug | last post by:
I have faced the problem that I want to hide the some rows of html table in the asp.net. For that I used the JavaScript but the problem is same I have to register the JavaScript function in the...
2
by: twink | last post by:
I am trying to access certain rows in a table in a combo box. I get this error (Run-time error '3265' Item not found in this collection) Here is the code. I am new to VBA so please excuse me. Here is...
2
by: kveerareddy | last post by:
How can i get the total rows of a table: If i use "select count(*) from <tablename>" it will take lot of time to get the total row count. for example in MSSQL we have "SELECT ROWS FROM...
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
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,...
1
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
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...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.