/    Sign up×
Community /Pin to ProfileBookmark

Java Script Cost Calculator

This script is supposed to add the cost of the items in the select menu, but it is only showing the total cost as the value of the last item in the menu. Here is the javascript I am using:

[CODE]<script language=”JavaScript”>

var Cost, Grand_Total;

function tally()
{
Cost = 0;
if (document.orderform.value=”Print1″) {Cost = 10}
if (document.orderform.value=”Print2″) {Cost = 20}
if (document.orderform.value=”Print3″) {Cost = 30}

document.orderform.Total.value = “£” + Cost;
}

function pound (amount)
{
amount = parseInt(amount * 100);
amount = parseFloat(amount/100);
if (((amount) == Math.floor(amount)) && ((amount – Math.floor (amount)) == 0))
{
amount = amount + “.00”
return amount;
}
if ( ((amount * 10) – Math.floor(amount * 10)) == 0)
{
amount = amount + “0”;
return amount;
}
if ( ((amount * 100) – Math.floor(amount * 100)) == 0)
{
amount = amount;
return amount;
}
return amount;
}

</script>[/CODE]

Here is the html that I am using:

[code=html]<table border=”0″>
<tr>
<td><label>
<select name=”prints”>
<option selected=”selected”>Options</option>
<option value=”Print1″ onClick=”tally()”>Print1</option>
<option value=”Print2″ onClick=”tally()”>Print2</option>
<option value=”Print3″ onClick=”tally()”>Print3</option>
</select>
</label></td>
<td>&nbsp;</td>
</tr>
<tr>
<td> Total</td>
<td><input type=”text” name=”Total” value=”&pound;0″ size=”7″ /></td>
</tr>
[/code]

Thanks in advance for any help, I am completly clueless about this sort of thing, but really need the script for my site!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangJun 25.2007 — Add themfunction tally()
{
Cost = 0;
if (document.orderform.value="Print1") {Cost [COLOR="Green"]+[/COLOR]= 10} // same as Cost = Cost + 10
if (document.orderform.value="Print2") {Cost [COLOR="Green"]+[/COLOR]= 20}
if (document.orderform.value="Print3") {Cost [COLOR="Green"]+[/COLOR]= 30}


<i> </i> document.orderform.Total.value = "£" + Cost;
<i> </i> }
Copy linkTweet thisAlerts:
@calumoggauthorJun 25.2007 — That didnt work, that just adds up all the cost values and gives a Total Cost of £60 for any selection. Sorry if I wasnt being clear enough, but I want it to show the cost of ONLY the selected item, not all the items.
Copy linkTweet thisAlerts:
@FangJun 25.2007 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;cost&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
var Cost, Grand_Total;

function tally(obj)
{
Cost = 0;
if (obj.value=="Print1") {Cost = 10}
if (obj.value=="Print2") {Cost = 20}
if (obj.value=="Print3") {Cost = 30}


<i> </i> document.orderform.Total.value = String.fromCharCode(163) + Cost;
<i> </i> }

function pound (amount)
{
amount = parseInt(amount * 100);
amount = parseFloat(amount/100);
if (((amount) == Math.floor(amount)) &amp;&amp; ((amount - Math.floor (amount)) == 0))
{
amount = amount + ".00"
return amount;
}
if ( ((amount * 10) - Math.floor(amount * 10)) == 0)
{
amount = amount + "0";
return amount;
}
if ( ((amount * 100) - Math.floor(amount * 100)) == 0)
{
amount = amount;
return amount;
}
return amount;
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form action="#" name="orderform"&gt;
&lt;div&gt;&lt;select name="prints" onchange="tally(this);"&gt; <br/>
&lt;option selected="selected"&gt;Options&lt;/option&gt; <br/>
&lt;option value="Print1"&gt;Print1&lt;/option&gt; <br/>
&lt;option value="Print2"&gt;Print2&lt;/option&gt; <br/>
&lt;option value="Print3"&gt;Print3&lt;/option&gt; <br/>
&lt;/select&gt;
&lt;label for="Total"&gt;Total &lt;input type="text" name="Total" id="Total" value="&amp;pound;0" size="7"&gt;&lt;/label&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@calumoggauthorJun 25.2007 — That worked fine! Thanks so much!!
×

Success!

Help @calumogg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...