rwur rwur - 1 month ago 4
jQuery Question

Table column index with previous rowspan columns

I have the following jsfiddle ready to show you my problem

`https://jsfiddle.net/rwurzer/no4sefyu/`


Live Demo

Basically the problem I have is a table column where I have rowspan assigned to.

I want to get the right column index.

I hope my fiddle is good enough to explain my problem :-)

Hope anyone can help?

Answer

Well, I know that you've already got an answer by try this variant. It's based on calculating indexes and should work for any cell. And I've removed duplicate id attributes in jsFiddle.

$('td').click(function(){
    var clickedEl = $(this);
    var myCol = clickedEl.closest("td").index();
    var myRow = clickedEl.closest("tr").index();
    var rowspans = $("td[rowspan]");
    rowspans.each(function () {
        var rs = $(this);
        var rsIndex = rs.closest("tr").index();
        var rsQuantity = parseInt(rs.attr("rowspan"));
        if (myRow > rsIndex && myRow <= rsIndex + rsQuantity - 1) {
            myCol++;
        }
    });
    alert('Row: ' + myRow + ', Column: ' + myCol);
});

Here is a demo in jsFiddle. Just want to solve your problem with this approach =).