CaffGeek CaffGeek - 1 month ago 13
jQuery Question

Removing columns, taking rowspan into account

I have the following example setup

http://jsfiddle.net/gibble/srBeB/

What I am trying to do, is remove the first two columns of a table

The issue is that the first column, contains cells that have a

rowspan='2'


So when I remove it, then go to the next row, it removes a cell it shouldn't

Thoughts on how to make this work?

Answer

Since row spans are involved, you'll have to change your strategy. Your table has four columns, so instead of removing the first two columns, you should aim to keep only the last two.

You can achieve that easily by passing a negative upper bound to slice():

$(this).children("td, th").slice(0, -2).remove();

You will find an updated fiddle here.