jQuey Adding dynamic variable (math)

I have a jQuery Function that loops, dynamically, depending on how many items are in a shopping cart.  For example, if there is one item, there is one jQuery function.  If there are two items, there are two jQuery functions, etc...  Each function creates a specific var called PRICE.   So, if I have three items in the cart the jQuery function loops three times and I have thee price vars. price_1, price_2, price_3, etc.  When the function is done looping I would like to add all three vars to get a total.  I have no idea where to start.
LVL 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Julian HansenCommented:
Do you have some code we can look at?
rgranlundAuthor Commented:
The code is to large and complex but every iteration of the loop I do this:
$('input[name="price[{row_id}]"]').val(premium_total);

the var is always (premium_total) within each loop.
Could I create an additional var that contains the total and then have a function that adds up each iteration of that var?
or something like:
$('.price').blur(function () {
    var sum = 0;
    $('.price').each(function() {
        sum += Number($(this).val());
    });

    // here, you have your sum
});​​​​​​​​​

Open in new window

Julian HansenCommented:
I sort of understand what you are trying to do - but not entirely.

The code you have posted will create the sum of all the elements with class='Price' - how that links to your premimum_total not sure. Also not sure what you mean when you say "Each function creates a specific var called PRICE."

Can you fill in some of the gaps?

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:
Your comments got me pointed in the correct direction.  Thanks for taking the time to help!!
Julian HansenCommented:
You are welcome.
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.