chandan chandan - 2 months ago 4x
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">
<td class="htInvalid">Bnagalore</td>
<td class="htInvalid">mysore</td>

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?


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()


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();