Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
4.20/5 (3 votes)
See more:
how can i select all diagonal cells(td) using jquery selector , suppose i have a nxn table and i want to select both '/' and '\' diagonals cells separately.

some thing like this

function getLDCount()
{
return $("#mytable td").......length
}

function getRDCount()
{
return $("#mytable td")..........length
}
Posted
Updated 25-Aug-12 19:01pm
v2

As the table is N x N - the length is obviously going to be N. To get the diagonal cells, the following logic would work:

Left to right:

You just need the cell whose index is equal to the row index

Right to left:

This is a bit involved than left to right. The following equation would get you the required cells:= N - 1 - Row<index>

Here is a jsfiddle for the same!

http://jsfiddle.net/kFP4D/[^]

Hope this helps!
 
Share this answer
 
v2
Comments
agha_ali22 26-Aug-12 1:20am    
lets suppose we have 5x5 table now can we use any jquery selector
Karthik. A 26-Aug-12 1:24am    
The jquery I've written will work for any N x N table. Try it out by adding a 4th & 5th column and a 4th & 5th row to that example!
try this.

JavaScript
$('#tableId tr').each(function(index){
       if ($(this).find('td:eq(' + (index) + ')').length == 1)
      a ++;
        var  tdLength = $(this).find('td').length;
      // alert(tdLength );

       if ($(this).find('td:eq(' + (tdLength - 1 - index) + ')').length == 1)
      b ++;


   });
   $("#mybutton").click(function(){
       alert(a);
       alert(b)
   });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900