contesa
asked on
Counting Rows in a Table
I want to count the rows in an html table as they are added. What's the easiest way using Javascript?
I only have 1 table in my document. There are two rows with column headings that I don't want to be included in the count.
I only have 1 table in my document. There are two rows with column headings that I don't want to be included in the count.
You add the rows to some parent. And that parent has an attribute: rows
That attribute is a collection and has an attribute: length
So the number of rows is: rows.length
That attribute is a collection and has an attribute: length
So the number of rows is: rows.length
ASKER
The script isn't working right for me. It only highlights the first row in the first table. I am not familiar with the table object.
Is there a shorter snippet of code that could count the rows?
Is there a shorter snippet of code that could count the rows?
Show your statement how you add a row and I show you how to count rows.
ASKER
<html><head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(p arseInt(ap pVersion)= =4)) {
document.MM_pgW=innerWidth ; document.MM_pgH=innerHeigh t; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_p gW || innerHeight!=document.MM_p gH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
</head>
<body>
<table name="list" id="list" width="700" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#0000FF">
<tr>
<td height="34" colspan="3" row=""><table width="100%" border="0">
<tr>
<td valign="bottom"><div align="left"><span class="style5">Total Count: </span></div> <div align="right"><br>
</div></td>
</tr>
</table></td>
</tr>
<tr>
<td row=""><div align="left"><strong>Last Name </strong></div></td>
<td row=""><div align="left"><strong>First Name </strong></div></td>
<td row=""><div align="left"><strong>Photo Date </strong></div></td>
</tr>
<tr>
<td colspan="3" row=""> <table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="33%"><a href="docs/abedrabbo_qasem _012505.pd f">Abedrab bo</a></td >
<td width="34%"><a href="docs/abedrabbo_qasem _012505.pd f">Qasem</ a></td>
<td width="33%"><a href="docs/abedrabbo_qasem _012505.pd f">01/25/0 5</a></td>
</tr>
<tr>
<td><a href="docs/abuolba_shadi_0 22106.pdf" >Abuolba</ a></td>
<td><a href="docs/abuolba_shadi_0 22106.pdf" >Shadi</a> </td>
<td><a href="docs/abuolba_shadi_0 22106.pdf" >02/21/06< /a></td>
</tr>
<tr>
<td><a href="docs/ackerman1_gary_ 022405.pdf ">Ackerman (1)</a></t d>
<td><a href="docs/ackerman1_gary_ 022405.pdf ">Gary</a> </td>
<td><a href="docs/ackerman1_gary_ 022405.pdf ">02/24/05 </a></td>
</tr>
<tr>
<td><a href="docs/ackerman_gary_0 22405.pdf" >Ackerman< /a></td>
<td><a href="docs/ackerman_gary_0 22405.pdf" >Gary</a>< /td>
<td><a href="docs/ackerman_gary_0 22405.pdf" >02/24/05< /a></td>
</tr>
<tr>
<td><a href="docs/adams_donald_11 3005.pdf"> Adams</a>< /td>
<td><a href="docs/adams_donald_11 3005.pdf"> Donald</a> </td>
<td><a href="docs/adams_donald_11 3005.pdf"> 11/30/05</ a></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(p
document.MM_pgW=innerWidth
else if (innerWidth!=document.MM_p
}
MM_reloadPage(true);
//-->
</script>
</head>
<body>
<table name="list" id="list" width="700" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#0000FF">
<tr>
<td height="34" colspan="3" row=""><table width="100%" border="0">
<tr>
<td valign="bottom"><div align="left"><span class="style5">Total Count: </span></div> <div align="right"><br>
</div></td>
</tr>
</table></td>
</tr>
<tr>
<td row=""><div align="left"><strong>Last Name </strong></div></td>
<td row=""><div align="left"><strong>First
<td row=""><div align="left"><strong>Photo
</tr>
<tr>
<td colspan="3" row=""> <table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="33%"><a href="docs/abedrabbo_qasem
<td width="34%"><a href="docs/abedrabbo_qasem
<td width="33%"><a href="docs/abedrabbo_qasem
</tr>
<tr>
<td><a href="docs/abuolba_shadi_0
<td><a href="docs/abuolba_shadi_0
<td><a href="docs/abuolba_shadi_0
</tr>
<tr>
<td><a href="docs/ackerman1_gary_
<td><a href="docs/ackerman1_gary_
<td><a href="docs/ackerman1_gary_
</tr>
<tr>
<td><a href="docs/ackerman_gary_0
<td><a href="docs/ackerman_gary_0
<td><a href="docs/ackerman_gary_0
</tr>
<tr>
<td><a href="docs/adams_donald_11
<td><a href="docs/adams_donald_11
<td><a href="docs/adams_donald_11
</tr>
</table></td>
</tr>
</table>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Since you mentioned about getting row count, and not sure how you want to apply /use it.
Here is code, which applies my script to your HTML code,
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) {
with (navigator) {
if ((appName=="Netscape")&&(p arseInt(ap pVersion)= =4)) {
document.MM_pgW=innerWidth ;
document.MM_pgH=innerHeigh t;
onresize=MM_reloadPage;
}
else if (innerWidth!=document.MM_p gW || innerHeight!=document.MM_p gH) {
location.reload();
}
}
}
}
function AlternateColors(tblObj, colorStr) {
if (!tblObj) { return; }
var colors=colorStr.split(',') ;
var cnt =1;
for (var trs=0; trs < tblObj.rows.length; trs++) {
trObj = tblObj.rows[trs];
trObj.style.backgroundColo r = colors[1-parseInt(cnt%2)];
cnt++;
if (cnt > 2) { cnt = 1; }
}
}
//-->
</script>
</head>
<body onLoad="MM_reloadPage(fals e);">
<table name="list" id="list" width="700" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#0000FF">
<tr>
<td height="34" colspan="3" row="">
<table width="100%" border="0">
<tr>
<td valign="bottom"><div align="left"><span class="style5">Total Count: </span></div> <div align="right"><br>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td row=""><div align="left"><strong>Last Name </strong></div></td>
<td row=""><div align="left"><strong>First Name </strong></div></td>
<td row=""><div align="left"><strong>Photo Date </strong></div></td>
</tr>
<tr>
<td colspan="3" row="">
<table id="list2" width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="33%"><a href="docs/abedrabbo_qasem _012505.pd f">Abedrab bo</a></td >
<td width="34%"><a href="docs/abedrabbo_qasem _012505.pd f">Qasem</ a></td>
<td width="33%"><a href="docs/abedrabbo_qasem _012505.pd f">01/25/0 5</a></td>
</tr>
<tr>
<td><a href="docs/abuolba_shadi_0 22106.pdf" >Abuolba</ a></td>
<td><a href="docs/abuolba_shadi_0 22106.pdf" >Shadi</a> </td>
<td><a href="docs/abuolba_shadi_0 22106.pdf" >02/21/06< /a></td>
</tr>
<tr>
<td><a href="docs/ackerman1_gary_ 022405.pdf ">Ackerman (1)</a></t d>
<td><a href="docs/ackerman1_gary_ 022405.pdf ">Gary</a> </td>
<td><a href="docs/ackerman1_gary_ 022405.pdf ">02/24/05 </a></td>
</tr>
<tr>
<td><a href="docs/ackerman_gary_0 22405.pdf" >Ackerman< /a></td>
<td><a href="docs/ackerman_gary_0 22405.pdf" >Gary</a>< /td>
<td><a href="docs/ackerman_gary_0 22405.pdf" >02/24/05< /a></td>
</tr>
<tr>
<td><a href="docs/adams_donald_11 3005.pdf"> Adams</a>< /td>
<td><a href="docs/adams_donald_11 3005.pdf"> Donald</a> </td>
<td><a href="docs/adams_donald_11 3005.pdf"> 11/30/05</ a></td>
</tr>
</table></td>
</tr>
</table>
<script language="javascript">
AlternateColors(document.g etElementB yId('list' ),"#CCFFAA , cyan");
AlternateColors(document.g etElementB yId('list2 '),"#CCCCC C, #00FF00");
</script>
</body>
</html>
Here is code, which applies my script to your HTML code,
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) {
with (navigator) {
if ((appName=="Netscape")&&(p
document.MM_pgW=innerWidth
document.MM_pgH=innerHeigh
onresize=MM_reloadPage;
}
else if (innerWidth!=document.MM_p
location.reload();
}
}
}
}
function AlternateColors(tblObj, colorStr) {
if (!tblObj) { return; }
var colors=colorStr.split(',')
var cnt =1;
for (var trs=0; trs < tblObj.rows.length; trs++) {
trObj = tblObj.rows[trs];
trObj.style.backgroundColo
cnt++;
if (cnt > 2) { cnt = 1; }
}
}
//-->
</script>
</head>
<body onLoad="MM_reloadPage(fals
<table name="list" id="list" width="700" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#0000FF">
<tr>
<td height="34" colspan="3" row="">
<table width="100%" border="0">
<tr>
<td valign="bottom"><div align="left"><span class="style5">Total Count: </span></div> <div align="right"><br>
</div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td row=""><div align="left"><strong>Last Name </strong></div></td>
<td row=""><div align="left"><strong>First
<td row=""><div align="left"><strong>Photo
</tr>
<tr>
<td colspan="3" row="">
<table id="list2" width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="33%"><a href="docs/abedrabbo_qasem
<td width="34%"><a href="docs/abedrabbo_qasem
<td width="33%"><a href="docs/abedrabbo_qasem
</tr>
<tr>
<td><a href="docs/abuolba_shadi_0
<td><a href="docs/abuolba_shadi_0
<td><a href="docs/abuolba_shadi_0
</tr>
<tr>
<td><a href="docs/ackerman1_gary_
<td><a href="docs/ackerman1_gary_
<td><a href="docs/ackerman1_gary_
</tr>
<tr>
<td><a href="docs/ackerman_gary_0
<td><a href="docs/ackerman_gary_0
<td><a href="docs/ackerman_gary_0
</tr>
<tr>
<td><a href="docs/adams_donald_11
<td><a href="docs/adams_donald_11
<td><a href="docs/adams_donald_11
</tr>
</table></td>
</tr>
</table>
<script language="javascript">
AlternateColors(document.g
AlternateColors(document.g
</script>
</body>
</html>
ASKER
Zvonko,
How do I display the total number of rows... how do I extract the results and display that number?
How do I display the total number of rows... how do I extract the results and display that number?
ASKER
Pravinasar,
I like the alternate colors... I am going to give it a try now.
I like the alternate colors... I am going to give it a try now.
ASKER
Pravinasar,
I copied and pasted your code and tested it... only the top row shows color. The other table (list2) is not showing any color at all.
I copied and pasted your code and tested it... only the top row shows color. The other table (list2) is not showing any color at all.
ASKER
Zvonko, the number is displaying - my oversight. However, it is only displaying the number 1.
Remove the space between two colors .. copy these lines..
I found out FF does not mind leading blank for the second color, but IE does not like it.
AlternateColors(document.g etElementB yId('list' ),"#CCFFAA ,cyan");
AlternateColors(document.g etElementB yId('list2 '),"#CCCCC C,#00FF00" );
I found out FF does not mind leading blank for the second color, but IE does not like it.
AlternateColors(document.g
AlternateColors(document.g
The id has to be in the INNER table: <table id="docs"
Like I did in my upper example.
Like I did in my upper example.
ASKER
Zvonko,
I copied it and pasted it into my document. Still not working.
Can I ask you a question? What does "docs" refer to?
I copied it and pasted it into my document. Still not working.
Can I ask you a question? What does "docs" refer to?
In my example is the "docs" the ID for the inner <table> containing five rows with the document names.
ASKER
Pravinsar,
Your alternate colors for row function is working like a champ... Thanks..
Since the original question referred to the counting of the rows. How can I award you points?
Zvonko has been helping me with my original question.
Your alternate colors for row function is working like a champ... Thanks..
Since the original question referred to the counting of the rows. How can I award you points?
Zvonko has been helping me with my original question.
ASKER
Zvonko,
I figured it out... had to include my table id where "docs" was located.
Thank you.
I figured it out... had to include my table id where "docs" was located.
Thank you.
You are welcome.
ASKER
Pravinsar,
Please advise if you would like me to create another problem so that I can award you full point value for your solution.
Thank you very much guys!
Please advise if you would like me to create another problem so that I can award you full point value for your solution.
Thank you very much guys!
Well it is up-to you.
Every thing has a purpose, and hence I showed with an example .
Also my first post mentioned..
>>Assuming you know the table Object,
>> tblObj.rows.length gives a rows in the table.
Points really does not matter to me.
Thanks,
Every thing has a purpose, and hence I showed with an example .
Also my first post mentioned..
>>Assuming you know the table Object,
>> tblObj.rows.length gives a rows in the table.
Points really does not matter to me.
Thanks,
ASKER
Pravinsar,
You're right...
I'm going to start a new question and I'd like you to repost your solution and I will award you the points. You earned them... and I really appreciate your help.
:-)
You're right...
I'm going to start a new question and I'd like you to repost your solution and I will award you the points. You earned them... and I really appreciate your help.
:-)
tblObj.rows.length gives a rows in the table.
Here is example which does color management for table rows.
<html>
<head>
<title>Alternate Table Row Colors</title>
</head>
<body>
<script language="javascript">
function AlternateColors(tblObj, colorStr) {
if (!tblObj) { return; }
var colors=colorStr.split(',')
var cnt =1;
for (var trs=0; trs < tblObj.rows.length; trs++) {
trObj = tblObj.rows[trs];
trObj.style.backgroundColo
cnt++;
if (cnt > 2) { cnt = 1; }
}
}
</script>
<table id="table1" border="1">
<tr><td>row one</td></tr>
<tr><td>row two</td></tr>
<tr><td>row three</td></tr>
<tr><td>row four</td></tr>
<tr><td>row five</td></tr>
<tr><td>row six</td></tr>
<tr><td>row seven</td></tr>
<tr><td>row eight</td></tr>
</table>
<table id="table2">
<tr><td>Row1 Cell 1</td><td>Row1 Cell 2</td></tr>
<tr><td>Row2 Cell 1</td><td>Row2 Cell 2</td></tr>
<tr><td>Row3 Cell 1</td><td>Row3 Cell 2</td></tr>
</table>
<script language="javascript">
AlternateColors(document.g
AlternateColors(document.g
</script>
</body>
</html>