chandan chandan - 4 months ago 9
jQuery Question

How to get a table td index based on the tr class name?

I have a table with the below details how to get the td index based on the tr class name

<Table id="#Hs_tbl">
<thead>
<th>country</th>
<th>state</th>
<th>city</th>
</thead>
<tr>
<td>india</td>
<td class="htInvalid">Bnagalore</td>
<td class="htInvalid">mysore</td>
</tr>
</table>


Now need the td tag index of based on the class="htInvalid"; here is what I have tried, I am able to get only one TD name, how to get both td names. Here is my code:

var col = $("#Hs_tbl tr").find("td." + "htInvalid").index();


How to get both the column name?

Answer

Either, You need to remove # from ID attribute of table.

<table id="Hs_tbl">

OR, You need to escape # meta character in selector like

$("#\\#Hs_tbl tr").find("td." + "htInvalid").index()

Docs

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.

To get all the all text, map() can be used

var alltdtext = $("#\\#Hs_tbl tr").find("td.htInvalid").map(function(){
   return $(this).text();
}).get();