1

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

3
  • .attr('class') instead of .text() Commented Jul 21, 2016 at 9:35
  • to get the href it's pretty simple in the each function do $(this).find("a").attr("class") Commented Jul 21, 2016 at 9:35
  • thinks, forgot about .attr('class') Commented Jul 21, 2016 at 9:40

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.