/    Sign up×
Community /Pin to ProfileBookmark

Help with adding number to a total inside a text box

Ok background on what I am trying to accomplish. First off this is just me trying to test this code if you can answerthe ultimate part thatd be GREAT. I am creating a page for 5 products(labled 1-5). I have my code to onkey stroke change the Total text box in the form. Once that is complete I have a selection drop down box that has 3 methods with different pricing. I will want to select an option and it selects a price for that value. ex: ground = 3.95. then you press calculate shipping and it addes Total plus shipping and displays in grand total box. here is my code thus far with the selection aprt even it in it.

right now I am just trying to get the code to add my total and 20 for testing but my output keeps coming in NaN any help would be GREAT>

script:
<script type=”text/javascript”>
/* <![CDATA[ */
var product1 = 0;
var product2 = 0;
var product3 = 0;
var product4 = 0;
var product5 = 0;
function calcproduct1(p1) {
product1 = p1 * 1.25;
calcTotalEstimate();
}
function calcproduct2(p2){
product2 = p2 *
.15;
calcTotalEstimate();
}
function calcproduct3(p3) {
product3 = p3 * 50;
calcTotalEstimate();
}
function calcproduct4(p4) {
product4 = p4 *
25;
calcTotalEstimate();
}
function calcproduct5(p5) {
product5 = p5 * 35;
calcTotalEstimate();
}
function calcTotalEstimate() {
var total = product1;
total += product2;
total += product3;
total += product4;
total += product5;
document.forms[0].total.value = “$” + total.toLocaleString();
}

function validateInput(keyPressEvent){
if (navigator.appName == “Microsoft Internet Explorer”)
var enteredKey = keyPressEvent.keyCode;
else if (navigator.appName == “Netscape”)
var enteredKey = keyPressEvent.charCode;
var enteredChar = String.fromCharCode(enteredKey);
var retValue = true;
try{
if (!/d/.test(enteredChar) && !/W/.test(enteredChar))
throw “You did not enter a numeric value.”;
}
catch(inputError) {
window.alert(inputError);
retValue = false;
}
finally {
return retValue;
}
}

function checkForNumber(fieldValue) {
var numberCheck = isNaN(fieldValue);
if (numberCheck == true) {
window.alert(“You must enter a numeric value!”);
return false;}
}

function calGrandTotal() {
grand = document.productForm.total.value
grand = Number(grand)
grand2 = 20
grand2 = Number(grand2)
grand3 = (grand + grand2)
document.productForm.grandTotal.value = grand3
}
/* ]]> */
</script>

and html code:
<body>
<form name=”productForm” action=”FormProcessor.html” method=”get” enctype=”application/x-www-form-urlencoded”
onsubmit=”return confirmSubmit();”
onreset=”return confirmReset();”>
<table border=”0″>
<tr><td>Product 1</td><td><input name=”p1″ type=”text” onkeypress=”return validateInput(event)” onblur=”calcproduct1(this.value);” /> ($1.25)</td></tr>
<tr><td>Product 2</td><td><input name=”p2″ type=”text” onkeypress=”return validateInput(event)” onblur=”calcproduct2(this.value);” /> (.15)</td></tr>
<tr><td>Product 3</td><td><input name=”p3″ type=”text” onkeypress=”return validateInput(event)” onblur=”calcproduct3(this.value);” /> ($50)</td></tr>
<tr><td>Product 4</td><td><input name=”p4″ type=”text” onkeypress=”return validateInput(event)” onblur=”calcproduct4(this.value);” /> ($25)</td></tr>
<tr><td>Product 5</td><td><input name=”p5″ type=”text” onkeypress=”return validateInput(event)” onblur=”calcproduct5(this.value);” /> ($35)</td></tr>
<tr><td>Total</td><td><input name=”total” type=”text” onkeypress=”return validateInput(event)” onfocus=”this.blur()” /> </td></tr>
<tr><td>Shipping Info</td><td><select name=”shippingMethod”>
<option selected=”selected” value=”ground”>Standard ground UPS</option>
<option value=”usps”>USPS</option>
<option value=”air”>2nd Day Air</option>
</select></td></tr>
<tr><td>Total W/ Shipping</td><td>
<input name=”grandTotal” type=”text” style=”height: 22px” readonly=”readonly” />&nbsp;
<input name=”shipTotalB” type=”button” value=”Calculate Total with shipping” onclick=”calGrandTotal(this.value)” /></td></tr>
</table>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@jdpjtp910authorFeb 20.2009 — any suggestions?
Copy linkTweet thisAlerts:
@iokosanFeb 20.2009 — <i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
/* &lt;![CDATA[ */

var prices = [1.25,0.15,50,25,35];
var sprices = [1,2,3];

function $(id){
return document.getElementById(id);
}

function calc(){
var tmp = 0;
for(var i = 1; i &lt;= prices.length; i++){
var a = parseInt($('p'+i).value);
tmp += (isNaN(a)?0:a) * prices[i-1];
}
return tmp;
}
function update(){
$('total').value ='$'+calc();
}

function validateInput(keyPressEvent){
if (navigator.appName == "Microsoft Internet Explorer")
var enteredKey = keyPressEvent.keyCode;
else if (navigator.appName == "Netscape")
var enteredKey = keyPressEvent.charCode;
var enteredChar = String.fromCharCode(enteredKey);
var retValue = true;
try{
if (!/d/.test(enteredChar) &amp;&amp; !/W/.test(enteredChar))
throw "You did not enter a numeric value.";
}
catch(inputError) {
window.alert(inputError);
retValue = false;
}
finally {
return retValue;
}
}

function checkForNumber(fieldValue) {
var numberCheck = isNaN(fieldValue);
if (numberCheck == true) {
window.alert("You must enter a numeric value!");
return false;}
}

function calGrandTotal() {
var tmp = calc();
tmp += sprices[$('sel').value];
$('gtotal').value = tmp;
}
/* ]]&gt; */
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name="productForm" action="FormProcessor.html" method="get" enctype="application/x-www-form-urlencoded"
onsubmit="return confirmSubmit();"
onreset="return confirmReset();"&gt;
&lt;table border="0"&gt;
&lt;tr&gt;&lt;td&gt;Product 1&lt;/td&gt;&lt;td&gt;&lt;input id="p1" type="text" onkeypress="return validateInput(event)" onblur="update();" /&gt; ($1.25)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Product 2&lt;/td&gt;&lt;td&gt;&lt;input id="p2" type="text" onkeypress="return validateInput(event)" onblur="update();" /&gt; (.15)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Product 3&lt;/td&gt;&lt;td&gt;&lt;input id="p3" type="text" onkeypress="return validateInput(event)" onblur="update();" /&gt; ($50)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Product 4&lt;/td&gt;&lt;td&gt;&lt;input id="p4" type="text" onkeypress="return validateInput(event)" onblur="update();" /&gt; ($25)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Product 5&lt;/td&gt;&lt;td&gt;&lt;input id="p5" type="text" onkeypress="return validateInput(event)" onblur="update();" /&gt; ($35)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Total&lt;/td&gt;&lt;td&gt;&lt;input id="total" type="text" onkeypress="return validateInput(event)" onfocus="update();" /&gt; &lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Shipping Info&lt;/td&gt;&lt;td&gt;
&lt;select name="shippingMethod" id='sel'&gt;
&lt;option selected="selected" value="0"&gt;Standard ground UPS&lt;/option&gt;
&lt;option value="1"&gt;USPS&lt;/option&gt;
&lt;option value="2"&gt;2nd Day Air&lt;/option&gt;
&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Total W/ Shipping&lt;/td&gt;&lt;td&gt;
&lt;input id="gtotal" type="text" style="height: 22px" readonly="readonly" /&gt;&amp;nbsp;
&lt;input name="shipTotalB" type="button" value="Calculate Total with shipping" onclick="calGrandTotal()" /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
Reply With Quote
&lt;/html&gt;


I tried to follow your "code".

Some suggestion, everything IMHO:

1) I'd use id instead of names

2) Use document.getElementById function

3) Remeber that this refers to the object which called the code: if you use this as an argument of onclick event, refered to a button, this would refear to that button,not to grandtotal.
×

Success!

Help @jdpjtp910 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.19,
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,
)...