JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 10th, 2012, 01:59 AM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 38 sec
Reputation Power: 1
Unhappy How can I create a function that calculate total price of products.

Hi there.

I wanted to know how to create a function that can keep adding all the sent parameters(numbers) by user clicks.

I'm having hard time figuring out how to create loops before writing actual calculation.

Actually, do I even have to write any kind of loops?

I want a page where users can see the list of products with each price on the sides, and users can click "add" button of multiple number of products, and number increases on the box called "Total Price" according to user clicks.

Can anyone please help me??
I'm new to JS..

Reply With Quote
  #2  
Old November 10th, 2012, 08:40 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 662 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 15 m 9 sec
Reputation Power: 69
Easiest way, would be something like this:

Code:
<script>
function total(amount)
{
var total = document.getElementById("total_storage").value;
total = parseInt(total) + parseInt(amount);
document.getElementById("total_storage").value="";
document.getElementById("total_storage").value = total;
document.getElementById("total_display").innerHTML = total;
}
function resetAll()
{
document.getElementById("total_storage").value = "0";
document.getElementById("total_display").innerHTML="0";
}
</script>

Total Price: $ <span id="total_display">0</span> || <a href="#" onclick="resetAll();return false">Clear Cart</a>

<br/>
<br/>

<input type="button" value="Add $10" onclick="total('10')"/>

<input type="button" value="Add $25" onclick="total('25')"/>

<input type="button" value="Add $100" onclick="total('100')"/>

<input id="total_storage" type="hidden" value="0"/>

Reply With Quote
  #3  
Old November 10th, 2012, 09:50 PM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 38 sec
Reputation Power: 1
Thank you very much!

Thanks web_loone08.

Yeah this makes sense after staring at the code for 20 minutes.

Now I have a stupid question.

Do you always have to clear the value of said id before storing new information into the value?
Can you not directly store total amount into the value that has been assigned to number "0" at the begging?

Thanks

Last edited by Kravvitz : November 11th, 2012 at 08:04 PM. Reason: must we quote the whole post? :p

Reply With Quote
  #4  
Old November 11th, 2012, 07:43 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 662 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 9 h 15 m 9 sec
Reputation Power: 69
You don't have to clear the value of the hidden input field each time, but you do need to reset it to "0"; when completely clearing out the "Total Price". Because if you don't; you are going to get a "NaN" or "Not a Number" error, with the code I have provided you.

Reply With Quote
  #5  
Old December 8th, 2012, 10:07 PM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 38 sec
Reputation Power: 1
Sorry for the late reply

Yeah that makes sense. Thank you very much




QUOTE=web_loone08]You don't have to clear the value of the hidden input field each time, but you do need to reset it to "0"; when completely clearing out the "Total Price". Because if you don't; you are going to get a "NaN" or "Not a Number" error, with the code I have provided you.[/QUOTE]

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How can I create a function that calculate total price of products.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap