Munawar
Munawar

Reputation: 2587

Jquery iterate table, having merged cells, rows and columns but skip first column

I am able to iterate all rows and columns but it does not work when table have merged cells. It does not go through the merged row for all normal columns(not having any merged cells).

My script:

$('#test tr').each(function() {
   $(this).find('td').each(function(colIndex)   
    {
        if(colIndex > 1)
        {
        $(this).css('background-color', 'red');
        }
    });   
});

A like example/test is available here:

Upvotes: 1

Views: 867

Answers (2)

Dolo
Dolo

Reputation: 976

$('#test tr').each(function() {



    var rowcount=0;
   $(this).find('td').each(function(colIndex)   
    {
        if(colIndex > 1)
        {
        $(this).css('background-color', 'red');
        }
      rowcount ++;
    }); 
  if(rowcount==2)
  {
     $(this).find('td').each(function(colIndex)   
    {
        if(colIndex == 1)
        {
        $(this).css('background-color', 'red');
        }

    });
  }


});

Modify your jquery like this. This will do. http://jsfiddle.net/dolours/pMWAw/26/

Upvotes: 1

Michael Peterson
Michael Peterson

Reputation: 1123

Can be simplified to one line - just use the :last-child selector

$('#test tr td:last-child').css('background-color', 'red');

http://jsfiddle.net/pMWAw/12/

Upvotes: 1

Related Questions