Help please solve the puzzle. How to calculate the number of td in a table, namely in a column with a specific class and with a specific value. The number of elements counts, but how to calculate exactly with the value 'ok'?

<table> <tr> <td>123</td> <td>123</td> <td>123</td> </tr> <tr> <td id='status'>123</td> <td id='status'>ok</td> <td id='status'>ok</td> </tr> 

 $.ajax({ method: "POST", url: './save', data: { id: idp, status: status}, success:function(res) { if(res == 'yes') { var nnn = $('.status').length; alert(nnn); } } }) 
  • In your example, there are no columns with a specific class. - Stepan Kasyanenko

1 answer 1

through selector: contains ('content')

 console.log($('.status:contains("ok")').length) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>123</td> <td>123</td> <td>123</td> </tr> <tr> <td class='status'>123</td> <td class='status'>ok</td> <td class='status'>ok</td> </tr> 

Instead of id use class, you can not repeat with id

UPDATE:

Stepan Kasyanenko wrote

You understand that this is not quite right? Because: contains ("ok") also counts, for example, cookies.

In this case, you can use filter

 const count = $('.status').filter(function () { return $(this).text().trim() === 'ok' }).length console.log(count) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>123</td> <td>123</td> <td>123</td> </tr> <tr> <td class='status'>123</td> <td class='status'>ok</td> <td class='status'>ok </td> <td class='status'>cookies</td> </tr> 

  • You understand that this is not quite right? Because :contains("ok") also counts, for example, cookies . - Stepan Kasyanenko
  • And why doesn’t it work for me? Counts the total number of cells in spite of the contents. What could it be? - I Zrchnsk
  • I have a class ... then manually wrote a question and hit on - I Zrchnsk
  • I don’t understand a bit, why should it count and cookies along with ok? - I Zrchnsk
  • co OK ies, that is, contains works if it finds a substring, not if both texts are the same - Ilya Zelenko