We help IT Professionals succeed at work.

jQuery Javascript

Robert Granlund
on
I'm not sure how to ask this question.  I have a jQuery function, that is re-iterated depending upon how many returns are on a page.

Here is what I am trying to do.  I would like to create a function that counts how many times the function loops and turn that number into a var.

function() {
  count (the amount of times the code loops.
var amount = AMOUNT OF TIMES
}

Open in new window


Then I want to include that specific var "amount" in each iteration of the function that loops
So if the script loops 3 times the var amount = 3;

Then in the looping script each iteration will include that specific var amount = 3

I have no idea how to start writing that and help/questions will be greatly apriciated.
Comment
Watch Question

Top Expert 2015

Commented:
many returns are on a page
What are "returns"?

If each "return" results in an element on the page of a particular class for example, then:

var amount = $('.elmentsOfClass').length;

Or, if the elements are divs for example, then:

var amount = $('div').length;
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
Would count how many times the function runs

var counter=0
function() {
 counter++
 return counter
}

Open in new window


How many times looped
var counter=0
function() {

for (i = 0; i < some_variable.length; i++) { 
    text += some_variable[i] + "<br>";
    counter++
}

 
 return counter

}

Open in new window

Author

Commented:
I am using a CMS called Expresssion Engine and it's Cart, CartThrob.  When you have items in your cart you have a tag pair
{exp:cartthrob:item_options entry_id="{entry_id}" row_id="{row_id}"}
ITEM Options
{/exp:cartthrob:item_options}

If you have three items in the cart, the tag will loop 3 times.  The row id increasing with each item.  The jQuery is also in this tag pair.  so the function would repeat 3 three times, containing variables unique to that specific iteration of the loop.  However, I need to count how many times it loops to create a specific /static  variable variable to insert into each iteration of the loop.  The variable I am trying to create is var "how many times the code loops"
If (var amount == '1') {
 msr = 1;
}
else If (var amount == '2') {
 msr = .90;
} else {
 msr = .85;
}

Open in new window



So, what ever var msr is, i insert it into each iteration of the looping function
Top Expert 2015
Commented:
I don't know about Scott, but I have zero experience with Expression Engine. After briefly looking at the documentation for Cartthrob however, I see they have a build-in variable for total_items in the cart. Seems like you could just use that instead of creating your own variable and incrementing it.

http://cartthrob.com/docs/tags_detail/cart_info/index.html#total_items

Author

Commented:
Of course, last night I was reading through the documentation thinking that there should be a built in function for this and found it.  I asked the question before doing all of the research I needed to do.  Thanks for the help and suggestions.