Home > Mobile >  jquery count 2 values in the table..?
jquery count 2 values in the table..?

Time:02-25

i want to count the value in my table and put the result to a div inside my Html document.

What i have is the following code:

    var counter = 0;
$(document).ready(function(){
    $('Table td').each(function(){
        if ($(this).text() === "Nicht aktiv") counter  ; 
        $("#log2").html("Nicht in Arbeit "   counter);
    });
});

I want to check:

if this text === "Nicht aktiv" and if this text in another td is Test1

Jquery or Javascript are not my main languages so i dont know, how i can solve this..

I hope you guys can help me.. :)

CodePudding user response:

You likely mean

$(function() {
  const counter = $('table td')
    .filter(function() {
      return $(this).text() === "Nicht aktiv" &&
        $(this).siblings().filter(function() {
          return $(this).text() === "Test1"
        }).length > 0;
    }).length
  $("#log2").html("Nicht in Arbeit "   counter);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="log2"></span>
<table>
  <tr>
    <td>Nicht aktiv</td>
    <td>Bla</td>
    <td>Test1</td>
  </tr>
  <tr>
    <td>Nicht aktiv</td>
    <td>Bla</td>
    <td>Test2</td>
  </tr>
  <tr>
    <td>Nicht aktiv</td>
    <td>Bla</td>
    <td>Test1</td>
  </tr>
  <tr>
    <td>Nicht aktiv</td>
    <td>Bla</td>
    <td>Test2</td>
  </tr>
  <tr>
    <td>Nicht aktiv</td>
    <td>Bla</td>
    <td>Test1</td>
  </tr>
</table>

  • Related