1

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.

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
Karthik
  • 1,155
  • 1
  • 13
  • 22

2 Answers2

3

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>
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
1

If you're always getting the second td tag that contains the value, you can just use the class selector to select the DOM element using $('.item-model-number .value').

If you want to get the second td tag without using the class name on it, you can do an nth of type selector using $('.item-model-number td:nth-of-type(2)').

To get the content of the DOM element, you can call .text() on the jQuery elements returned by the selector so the whole thing would look like $('.item-model-number .value').text() or $('.item-model-number td:nth-of-type(2)').text()

Snags88
  • 86
  • 3