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.

0

2 Answers 2

4

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>

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

Comments

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

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.