Search code examples
jqueryhtmlhtml-tablehtml-tableextract

How to get TD cell values using jquery


I would like to get the text from the first and second TD which has the class user and id

<tr class="item-model-number">
  <td class="label">Item model number</td>
  <td class="value">GL552VW-CN426T</td>
</tr>

I have tried this jQuery code but didn't work for me:

$(".item-model-number").find("td").each(function() {
  var test = $(this).text();
  alert(test);
}

I want to retrieve GL552VW-CN426T from inside the second td tag in the tr.


Solution

  • You just need to amend your selector to get the correct element:

    $(".item-model-number .value").each(function() {
      var value = $(this).text();
      console.log(value);
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <tr class="item-model-number">
        <td class="label">Item model number</td>
        <td class="value">GL552VW-CN426T</td>
      </tr>
    </table>