There is a column with lines in which two values ​​are put (Yes and No), these values ​​change dynamically for me, I want to count these words at the end of the table and at the same time, if I change the values, the output with the words is also recalculated. How to do it ? Maybe tell me the article. I want to crank the whole thing on JS, Jquery
Yes tables are stored in the database
data-id - in the cell means the line number
data-name - the name of the table column in the database

I have the editing mechanism as follows, if in the row of the table there is a class Mult, when I click on this cell with a class, I create the Select option and select a value. As soon as I select the values ​​in the column, I want to calculate how many values ​​I have. Yes - Count and No-Count

enter image description here


I found something similar that I need. An example here is counting all the words, but I need to count the specific words Yes and No.

  • It's all great ... and where is the table? in what format? where does the data come from and what is the mechanism for changing them? - Akina

1 answer 1

Wrote an example on jQuery. Use each() - iterate over the elements of the array, and text() - access the text inside the element

 var yes = 0 var no = 0 $('td.mult').each(function() { if ($(this).text() == 'Да') { yes++ } if ($(this).text() == 'Нет') { no++ } }) document.write('Да: ' + yes + ' Нет: ' + no) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td class="mult">Да</td> <td class="mult">Нет</td> <td class="mult">Да</td> <td class="mult">Да</td> </tr> </table> 

  • And how can I update the result of the obtained values? I just edit the table dynamically. And to see the changes in the words yes or no, you have to refresh the page, I would like to get around this case) - Eliot
  • one
    Yes of course. Instead of document.write , you will have something like $('div.myclass').text('Да: ' + yes + ' Нет: ' + no) , and place the code in the function that you call after making the changes. - Crantisz
  • thank you, everything turned out as planned) - Eliot