blob84 Posted December 16, 2009 Share Posted December 16, 2009 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> Link to comment Share on other sites More sharing options...
Ingolme Posted December 16, 2009 Share Posted December 16, 2009 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. Link to comment Share on other sites More sharing options...
blob84 Posted December 16, 2009 Author Share Posted December 16, 2009 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 Link to comment Share on other sites More sharing options...
Ingolme Posted December 16, 2009 Share Posted December 16, 2009 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 More sharing options...
blob84 Posted December 16, 2009 Author Share Posted December 16, 2009 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> Link to comment Share on other sites More sharing options...
Ingolme Posted December 16, 2009 Share Posted December 16, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.