Tanvir Alam Tanvir Alam - 17 days ago 5
HTML Question

Jquery parse HTML Table

I have the following table:

<table id="tableData" name="tableData">
<tr>
<td class="tdvalue"><a class="XXX">10</a></td>
<td class="tdvalue"><a class="YYY">4</a></td>
<td class="tdvalue"><a class="XXX">7</a></td>
</tr>
</table>


I need to get the td value which has hyperlink with class name in an array

The following code is not working

var rows = [];
$("#tableData tr").each(function() {
$tr = $(this);
var row = [];
$tr.find(".tdvalue").each(function(){
row.push($(this).text());
});
});


This gives the value of the text with in the td.
but I need to class name of XXX
<a class="XXX">10</a>


result would be:

Array[3] 0:"XXX" 1:"YYY" 2:"XXX"


Please help anyone

Answer

target the a's directly:

var row = [];

$(".tdvalue a").each(function() {
      row.push($(this).attr('class'));
    });

//gives row= ["XXX","YYY","XXX"]

var row = [];
    
$(".tdvalue a").each(function() {
      row.push($(this).attr('class'));
   });

console.log(row)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tableData" name="tableData">
    <tr>
        <td class="tdvalue"><a class="XXX">10</a></td>
        <td class="tdvalue"><a class="YYY">4</a></td>
        <td class="tdvalue"><a class="XXX">7</a></td>
    </tr>
</table>

Comments