Question: I have multiple column headers that spans of multiple column and I would like to find the correct header number/count for the columns.

I want to enter a column number and get the header: example column 5 is RDataTest6 $('td.eq(5)) and its header is HTest3 $('th.eq(2))

---

Example: 

```
<table>
    <tr>
        <th>HTest1</th>
        <th colSpan="2">HTest2</th>
        <th colSpan="4">HTest3</th>
        <th colSpan="2">HTest4</th>         
    </tr>
    <tr>
        <td>RDataTest1</td>
        <td>RDataTest2</td>
        <td>RDataTest3</td>
        <td>RDataTest4</td>
        <td>RDataTest5</td>
        <td>RDataTest6</td>
        <td>RDataTest7</td>
        <td>RDataTest8</td>
        <td>RDataTest9</td>
    </tr>
</table>

```

​

Answer: 
Edit: You should implement a simple array that hold the header location, see below,

```
var thLocator = [], colCount = 1;
$table.find('tr:first th').each(function () {
    for (var i = 0; i < this.colSpan; i++) {
        thLocator.push(colCount);
    }
    colCount++;
});

$table.find('td').click(function () {
    alert(thLocator[$(this).index()]);
});

```

And then anytime You can get the location of a td column. See [**DEMO**](http://jsfiddle.net/skram/dHKgn/3/) -> Click on any TD to identify its col head position.

---

I am not sure which count you want so I wrote down all col count. See [**DEMO**](http://jsfiddle.net/skram/dHKgn/2/) 

```
$(function() {
    var $table = $('table');

    alert('Table Header Count ' + $table.find('tr:first th').length);

    var thCount = 0;
    $table.find('tr:first th').each(function () {
        thCount += this.colSpan;
    });

    alert('Computed TH Count ' + thCount );

    alert('Table TD Col Count ' + $table.find('tr:eq(1) td').length);
});

```
That's easy, as long as you don't have any `rowspan`s:

```
function getCell(tr, col) {
    var cell = tr.firstElementChild;
    while (cell && (col -= cell.colSpan || 1)>=0)
        cell = cell.nextElementSibling;
    return cell;
}

```

See [demonstration](http://jsfiddle.net/nx5Ab/).