Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JQuery Syntax

Posted on 2015-02-17
6
81 Views
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;
				}
 	alert(premium);
 			$( "#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

0
Comment
Question by:rgranlund
  • 3
  • 2
6 Comments
 
LVL 33

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!
0
 
LVL 7

Author Comment

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

Expert Comment

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

var finalTotal;
0
 
LVL 7

Author Comment

by:rgranlund
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;
alert(finalTotal);
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.
0
 
LVL 33

Accepted Solution

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

var finalTotal = 0;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
$q.all is slower or not  in  Angularjs 2 21
ajax jquery 3 25
suddenly angular cli doesn't work. 5 38
jquery check value of radiobutton and checkboxlist 3 28
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

790 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