jQuery Javascript

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.
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
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;
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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

0
rgranlundAuthor 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
0
Tom BeckCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

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.