Jump to content

Count Cells


blob84
 Share

Recommended Posts

HI, i have these loops, i need to count cells from north to south-east,why doesn't it counts the 3, 4, 5 cells??

<html><head><script type="text/javascript">function celle() {var table = document.getElementById("table");for(y=0; y < 6; y++) {for(i=0; i < 6; i++) {//alert(table.rows[i].cells[i+y].innerHTML);alert(i+y);}//alert(i+y);}}</script></head><body><input type="button" value="bottone" onclick="celle();" /><table id="table" border="1">			<tr>		<td> 1 </td> <td> a</td> <td> b</td> <td> c</td> 	<td> d</td> <td> e</td> 	<td> f</td>	</tr>		<tr> 	<td>2 </td> <td> h</td> <td> i</td> <td> l</td> <td> m</td> <td> n</td> <td> o</td> </tr>		<tr> <td> 3</td> <td>p </td> <td> q</td> <td> r</td> <td> s</td> <td> t</td> <td> u</td>	</tr>		<tr> 	<td>4 </td> <td> v</td> <td> z</td> <td> ab</td> <td>cd </td> <td> ef</td> <td>tutti </td>	</tr>		<tr> 	<td> 5</td> <td> hi</td> 	<td> lm</td> <td> no</td> <td> pq</td> 	<td> rs</td> <td> a</td>	</tr>	<tr> <td> 6</td> <td> tu</td> 	<td> vz</td> <td> 12</td> <td> 11</td> 	<td> 10</td> <td> ciao</td> </tr>		</table></body></html>

Edited by blob84
Link to comment
Share on other sites

Why not use cells[y] instead of cells[i+y]? I'm pretty sure you won't be able to get all the cells since as soon as i is greater than 0 you're already missing the first cell of the row.
I want to count like this:x  x	x	  xif i use only cells[i]  i get:x xxxx

Edited by blob84
Link to comment
Share on other sites

The way you have it currently checks the rows something like this:

x x x x x x|  x x x x x| x	x x x x| x x	  x x x| x x x		x x| x x x x		  x| x x x x x

You only need one loop to do what you're asking for:

for(i=0; i < 6; i++) {  alert(table.rows[i].cells[i].innerHTML);}

Link to comment
Share on other sites

ok,I want to start from the second row(index 1) and do something like this:but it doesn't count the cells [i+2], like the loops nested, why??? :)

<script type="text/javascript">function celle() {var table = document.getElementById("table");for(i=1; i <= 6; i++) {alert(table.rows[i].cells[i-1].innerHTML);}for(i=1; i <=6; i++) {alert(table.rows[i].cells[i].innerHTML);}for(i=1; i <=6; i++) {alert(table.rows[i].cells[i+1].innerHTML);}for(i=1; i <= 6; i++) {alert(table.rows[i].cells[i+2].innerHTML);}}</script>

Edited by blob84
Link to comment
Share on other sites

I don't really understand what you're asking for.If you want to start from the second row, start i at 1. Subtract 1 from the cell if you want it to start from the first cell in the row.

for(i=1; i < 6; i++) {  alert(table.rows[i].cells[i-1].innerHTML);}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...