Select td based on text

Asked

Viewed 191 times

1

I have a table where I want to select the td's that contain certain text. I have tried options such as:

console.log($("#minhaTabela").find("td[value='A']"));
console.log($("#minhaTabela").find("td[text='A']"));
console.log($("#minhaTabela").find("td[innerText='A']"));
console.log($("#minhaTabela").find("td[context='A']"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="minhatabela">
  <tr>
    <td>A</td>
    <td>B</td>
  </tr>
</table>

All the above options return nothing. How can I do this?

1 answer

3


Well, the table id was wrong in the code (in camelCase when the html is in low box). CSS is not able to filter by content, the [] is only valid for attributes.

But since you’re using this inside jQuery, you can use the :contains()

console.log($("#minhatabela").find("td:contains('A')"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="minhatabela">
  <tr>
    <td>A</td>
    <td>B</td>
  </tr>
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.