JQuery Syntax

Posted on 2015-02-17
Last Modified: 2015-02-18
I have a piece of JQuery that loops.  Depending upon how many returns are on a page determines how many time the JQuery Loops.  What the script does is adds up numbers for each specific iteration of the loop.

So, if there were three iterations of the loop I may have a return of  10, 5 and 13.

The whole script is too long to put on here but the end looks like this:
	if(premium_total < 100) {
					premium = 100;
				} else {
					premium = premium_total;
 			$( "#result" ).html( premium );
 			$( "#price{row_id}" ).val( premium );	
 	//  END PRINT Final Price		

Open in new window

So after the script has looped, I want to take the total of all of the "Premium" for a final total.  So in my example here, my total will be 28.

I need something like:
var finalTotal = TOTAL of all of the Premium Vars

Open in new window

Question by:rgranlund
  • 3
  • 2
LVL 32

Expert Comment

by:Big Monty
ID: 40615414
at the end of your loop, just put:

finalTotal += premium;

remember to declare finalTotal above the loop first!

Author Comment

ID: 40616678
@BigMonty  I don't understand what you mean by "remember to declare finalTotal above the loop first! "
LVL 32

Expert Comment

by:Big Monty
ID: 40616726
before your loop code, declare the variable like:

var finalTotal;

Author Comment

ID: 40616802
OK.  I got that now.  Thanks./  However, when I run the code, the var premium gives me a number value, but:
finalTotal += premium;
The alert gives me NaN.

Not sure what I am doing wrong.  I have researched but I am still too much of a JQuery Newbie to figure it out.
LVL 32

Accepted Solution

Big Monty earned 500 total points
ID: 40616808
try initializing the variable to 0:

var finalTotal = 0;

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
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)

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now