shoaib shoaib - 21 days ago 5
jQuery Question

How to get value of nextsibling if element search by specific string?

I am trying to get the text which is in the next element of searched element by string.Let me explain by code



<table id="myTable">
<tbody>
<tr>
<th>Name</th>
<td>foo</td>
</tr>
<tr>
<th>age</th>
<td>20</td>
</tr>
</tbody>
</table>





I have to search string if "age" exist. then return 20 as its value.
I tried to search string by contains: But unable to access value

Answer

You could use jQuery next() and contains selector to achieve what you need.

$(document).ready(function() {
  console.log($("th:contains(age)").next().html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
  <tbody>
    <tr>
      <th>Name</th>
      <td>foo</td>
    </tr>
    <tr>
      <th>age</th>
      <td>20</td>
    </tr>
  </tbody>
</table>