?
Solved

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

Posted on 2011-10-09
8
Medium Priority
?
240 Views
Last Modified: 2012-05-12
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
Comment
Question by:areyouready344
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36940263
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
 

Author Comment

by:areyouready344
ID: 36940288
but how do I get the previous loop count in a current loop?
0
 
LVL 83

Expert Comment

by:leakim971
ID: 36940291
it's loopCount value
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:areyouready344
ID: 36940310
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
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36940313
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
 

Author Comment

by:areyouready344
ID: 36940345
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
 
LVL 83

Expert Comment

by:leakim971
ID: 36940362
Put the alert before if(start > finish)
0
 
LVL 83

Expert Comment

by:leakim971
ID: 36946386
no?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question