var k = 0; function add() { $('tr').each(function(row) { var col1 = $(row).children('td:nth-child(1)').text(); var col2 = $(row).children('td:nth-child(2)').text(); if (col1 == 'опоздание более 15мин' && col2 == 'согласовано') { console.log(col1, col2); return ++k; } }); } console.log(add()); $('span.str').text('' + k); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border=1> <tr> <td>опоздание более 15мин</td> <td>согласовано</td> </tr> </table> <span class="str"></span> 

  • one
    You choose something from html, but it was not shown - Alexey Shimansky
  • @ Alexey Shimansky, corrected. - Eliot
  • @Kromster, 1 and 2. These numbers indicate the order of the columns in fact and that's it. - Eliot

2 answers 2

Try this, because the f-tion each finds an array of elements (even if it is 1), the first parameter is the index of the element in the array, and the second element, you tried to use the index instead of the element. Also wait for jQuery to load.

 <script> var k=0; jQuery(function($){ console.log(add()); $('span.str').text(''+k); }); function add(){ $('table tr').each(function (row, e) { var col1 = $(e).children('td:nth-child(1)').text(); var col2 = $(e).children('td:nth-child(2)').text(); if (col1 == 'опоздание более 15мин' && col2 == 'согласовано') { console.log(col1, col2); return ++k; } }); } </script> 
  • what you need works correctly. Thanks you. - Eliot

In the each function there should be 2 variables: the first is the sequence number, the second is the object itself.

 var k = 0; function add() { $('tr').each(function(nomer, row) { var col1 = $(row).children('td:nth-child(1)').text(); var col2 = $(row).children('td:nth-child(2)').text(); if (col1 == 'опоздание более 15мин' && col2 == 'согласовано') { console.log(col1, col2); return ++k; } }); } add(); $('span.str').text('' + k); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border=1> <tr> <td>опоздание более 15мин</td> <td>согласовано</td> </tr> </table> <span class="str"></span>