Keep count of jquery function actions












-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










share|improve this question


















  • 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 3 at 1:49








  • 1





    Btw, don't try to declare variable names more than once

    – CertainPerformance
    Jan 3 at 1:51











  • I am trying to check if any rows in my table exist

    – Uskompuf
    Jan 3 at 1:52








  • 2





    Possible duplicate of 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 I mentioned above. :)

    – Tyler Roper
    Jan 3 at 1:54








  • 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 3 at 1:57


















-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










share|improve this question


















  • 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 3 at 1:49








  • 1





    Btw, don't try to declare variable names more than once

    – CertainPerformance
    Jan 3 at 1:51











  • I am trying to check if any rows in my table exist

    – Uskompuf
    Jan 3 at 1:52








  • 2





    Possible duplicate of 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 I mentioned above. :)

    – Tyler Roper
    Jan 3 at 1:54








  • 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 3 at 1:57
















-2












-2








-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










share|improve this question














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







javascript jquery html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 1:45









UskompufUskompuf

947




947








  • 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 3 at 1:49








  • 1





    Btw, don't try to declare variable names more than once

    – CertainPerformance
    Jan 3 at 1:51











  • I am trying to check if any rows in my table exist

    – Uskompuf
    Jan 3 at 1:52








  • 2





    Possible duplicate of 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 I mentioned above. :)

    – Tyler Roper
    Jan 3 at 1:54








  • 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 3 at 1:57
















  • 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 3 at 1:49








  • 1





    Btw, don't try to declare variable names more than once

    – CertainPerformance
    Jan 3 at 1:51











  • I am trying to check if any rows in my table exist

    – Uskompuf
    Jan 3 at 1:52








  • 2





    Possible duplicate of 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 I mentioned above. :)

    – Tyler Roper
    Jan 3 at 1:54








  • 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 3 at 1:57










2




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 3 at 1:49







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 3 at 1:49






1




1





Btw, don't try to declare variable names more than once

– CertainPerformance
Jan 3 at 1:51





Btw, don't try to declare variable names more than once

– CertainPerformance
Jan 3 at 1:51













I am trying to check if any rows in my table exist

– Uskompuf
Jan 3 at 1:52







I am trying to check if any rows in my table exist

– Uskompuf
Jan 3 at 1:52






2




2





Possible duplicate of 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 I mentioned above. :)

– Tyler Roper
Jan 3 at 1:54







Possible duplicate of 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 I mentioned above. :)

– Tyler Roper
Jan 3 at 1:54






1




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 3 at 1:57







@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 3 at 1:57














1 Answer
1






active

oldest

votes


















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.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015368%2fkeep-count-of-jquery-function-actions%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer






























      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.






      share|improve this answer




























        2












        2








        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.






        share|improve this answer















        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.






        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>





        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>





        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>





        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>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 3 at 2:27

























        answered Jan 3 at 1:56









        showdevshowdev

        20.1k133352




        20.1k133352
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015368%2fkeep-count-of-jquery-function-actions%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'