-2

I have the following jquery function

function checkIfAnyExist() { //new input
    var number = 0;
    if ($(this).length) {
      var number = number + 1;
    }
    console.log(number);
}

Which i call using:

$("#myTable tr:not(.discarded,.old) td:nth-child(1)").each(checkIfAnyExist);

However this code doesn't work as expected as for every row number is reset. I tried taking var number = 0 out and putting it above call function but that didn't work either.

Any ideas?

Thanks

Luke Prior
  • 885
  • 2
  • 11
  • 34
  • 2
    Can you maybe take a step back and explain *why* you need this counter? I worry this may be an XY problem. If you're trying to count the elements, you simply need `.length`. (Not to mention, `$(this).length` in your example will always return `1`, thus `true`) – Tyler Roper Jan 03 '19 at 01:49
  • 1
    Btw, don't try to declare variable names more than once – CertainPerformance Jan 03 '19 at 01:51
  • I am trying to check if any rows in my table exist – Luke Prior Jan 03 '19 at 01:52
  • 2
    Possible duplicate of [Is there an "exists" function for jQuery?](https://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery) And in the future, always be sure to explain the bigger picture when asking for help, so you can avoid the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) I mentioned above. :) – Tyler Roper Jan 03 '19 at 01:54
  • problem is i need to check if any of my rows exist not just check one – Luke Prior Jan 03 '19 at 01:56
  • 1
    @Uskompuf If your selector finds `10` rows, then `.length` will return `10`. By doing `if ($("selector").length)`, it will return `false` if there are none, or `true` if there are one **or more**. – Tyler Roper Jan 03 '19 at 01:57

1 Answers1

2

You are declaring the variable twice: once in the function and once more inside the if condition. Both of those definitions reset the variable to zero upon each iteration of the function.

Consider declaring number once, outside the function's scope. That way, it can be incremented each time the function is called.

function checkIfAnyExist() {
  if ($(this).length) {
    number++;
  }
  console.log(number);
}

var number = 0;

$("#myTable tr:not(.discarded,.old) td:nth-child(1)").each(checkIfAnyExist);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<table id="myTable">
  <tr><td></td></tr>
  <tr><td></td></tr>
  <tr><td></td></tr>
  <tr><td></td></tr>
  <tr class="discarded"><td></td></tr>
  <tr class="old"><td></td></tr>
</table>

You are then counting the number of "first cell"s in rows that are not .discarded or .old.

As @TylerRoper wisely explained, $(this).length will never be false in this situation; every row passed to the function has a length. So the function does not appear to be useful.

jQuery's .length by itself is a more effective method of counting the appropriate rows:

var rowCount = $("#myTable tr:not(.discarded,.old)").length;
console.log(rowCount ? rowCount + ' rows found.' : 'There are no rows.');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<table id="myTable">
  <tr></tr>
  <tr></tr>
  <tr></tr>
  <tr></tr>
  <tr class="discarded"></tr>
  <tr class="old"></tr>
</table>

Note: It might be a different story if you're trying to determine how many rows are not empty, or contain more than one cell, or contain at least one cell with content, or some other more complicated selection.

showdev
  • 28,454
  • 37
  • 55
  • 73