2

"I want the index of id:Second to be 2(Considering #First as 2 columns). But with the help of $("#Second").parent().find("td").index($("#Second")) i get 1. Any Direct function in Jquery to do this without i having to use FOR LOOPS or .each function or any sort of loop???"

<table>
<tr>
<td colspan="2" id="First">
</td>
<td colspan="2" id="Second">
</td>
</tr>
</table>
user3286962
  • 147
  • 2
  • 10

1 Answers1

10

Any Direct function in Jquery to do this without i having to use FOR LOOPS or .each function or any sort of loop???"

No, I don't believe there is. The loop is trivial, of course.

var index = 0;
$("#Second").prevAll("td").each(function() {
    index += this.colSpan;
});
console.log(index);

Output:

2

...which is the 0-based index of that cell, including colspans.

Live Example | Source

Similarly, with this:

<table>
    <tr>
        <td>One column wide</td>
        <td colspan="2">Two columns wide</td>
        <td>One column wide</td>
        <td colspan="2" id="Last">Two columns wide</td>
    </tr>
</table>

That code gives us 4 for the last column (the 0-based index of the 5th column). Live Example | Source

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • There is a similar question here: http://stackoverflow.com/questions/1166452/finding-column-index-using-jquery-when-table-contains-column-spanning-cells/26896740#26896740 I love how concise this is, thanks. – Alan B. Dee Nov 12 '14 at 21:18