Consider the following table:
<table>
<thead>
<tr>
<th scope="col" />
<th scope="col">A</th>
<th scope="col">B</th>
<th scope="col">C</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Apples</td>
<td>Oranges</td>
<td>Pears</td>
</tr>
<tr>
<th scope="row">2</th>
<td rowspan="2">Subcategory Heading</td>
<td>ASP.Net</td>
<td>Other</td>
</tr>
<tr>
<th scope="row">3</th>
<td>MVC</td>
<td>PHP</td>
</tr>
<tr>
<th scope="row">4</th>
<td>Red</td>
<td>Green</td>
<td>Blue</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4">Some pointless footer content</td>
</tr>
</tfoot>
</table>
How would I retrieve the correct column number for the cell containing the text "MVC" or "PHP" using jQuery? The correct answer would be column 3, but just using .prevAll().length
will only count the number of previous cells, which will not accurately reflect their actual column position in this case?
I'm sure I'll need to use each()
, but I'm looking for the most efficient implementation as well.