• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

how to keep track of a previous loop count using jquery?

how to keep track of a previous loop count using jquery?

for example,

$("#list li").filter:gt('+ variable+').each(function(
{
       if(start > finish)
       {
            loopCount = loopCount + 1;
            $('#list2 li').append('<li>' + variable + '</li>');
       }

}))

Now if I go through this each loop again, I need to know what was the previous loop count.
0
areyouready344
Asked:
areyouready344
  • 5
  • 3
1 Solution
 
leakim971PluritechnicianCommented:
put the variable outside the each or as global variable :

var loopCount = 0;
$("#list li").filter:gt('+ variable+').each(function(
{
       if(start > finish)
       {
            loopCount = loopCount + 1;
            $('#list2 li').append('<li>' + variable + '</li>');
       }

}))

Open in new window


or :


var loopCount = 0;
var loopCount = 0;
$(document).ready(function() {

$("#list li").filter:gt('+ variable+').each(function(
{
       if(start > finish)
       {
            loopCount = loopCount + 1;
            $('#list2 li').append('<li>' + variable + '</li>');
       }

}))

})

Open in new window

0
 
areyouready344Author Commented:
but how do I get the previous loop count in a current loop?
0
 
leakim971PluritechnicianCommented:
it's loopCount value
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
areyouready344Author Commented:
Let say for example, I have the following code

$("#click").click(function(
{
      var loopCount = 0;

       $("#list li").filter:gt('+ variable+').each(function(
       {
             if(start > finish)
            {
                  loopCount = loopCount + 1;
                 $('#list2 li').append('<li>' + variable + '</li>');
             }
          });
});

During each click of button, I want the loopCount to start at zero and count up. Upon additional clicks, I need to know what was the previous loopCount.
0
 
leakim971PluritechnicianCommented:
var lastLoopCountValue = null;
$(document).ready(function() {

$("#click").click(function(
{
      var loopCount = 0;

       $("#list li").filter:gt('+ variable+').each(function(
       {
             if(start > finish)
            {
                  loopCount = loopCount + 1;
                 $('#list2 li').append('<li>' + variable + '</li>');
             }
          });
       lastLoopCountValue = loopCount;
});
0
 
areyouready344Author Commented:
Thanks leakim but I wondering how to get lastLoopCountValue to read null during the first iteration of the loop.

var lastLoopCountValue = null;
$(document).ready(function() {

$("#click").click(function(
{
      var loopCount = 0;

       $("#list li").filter:gt('+ variable+').each(function(
       {
             if(start > finish)
            {
                  loopCount = loopCount + 1;
                 $('#list2 li').append('<li>' + variable + '</li>');
             }else
             {
                  alert(lastLoopCountValue); // its not reading a null value during the first iteration of the loop, which what I was expecting.
                  alert(loopCount);
             }
          });
       lastLoopCountValue = loopCount;
});
0
 
leakim971PluritechnicianCommented:
Put the alert before if(start > finish)
0
 
leakim971PluritechnicianCommented:
no?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now