Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript - Numbers not adding up?

Status
Not open for further replies.

rxsid

Programmer
Jul 9, 2002
57
US
Hi all,

I'm trying to add up all numbers entered on a child form, then populate that sum into a field on the parent form. I can transfer a single number only. When multiple numbers (from multiple text fields on the child) are filled in...they are "stringed" together onto the parent form. Not added together.

Here's my javascript code so far:

function fillValue() {
window.opener.document.forms[0].elements[1].value = ""
var rentTotal = ""
var tempTotal = ""
var elemLen = myform.elements.length
for (var x=0;x<elemLen;x++) {
//alert(myform.elements[x].value)
tempTotal = myform.elements[x].value
if (tempTotal > 0) {
theTotal = theTotal + tempTotal
//Also tried these
//theTotal = (theTotal + tempTotal)
//theTotal =+ tempTotal
}
}
window.opener.document.forms[0].elements[1].value = theTotal
return true;
}


Any ideas on why the numbers aren't being added, but instead being concatenated together in a string?

Thanks.
 
The + symbol acts as a concatenator operator when dealing with strings, and a regular addition operator when dealing with numbers. You just need to turn the variable(s) into numbers using the parseInt() method.

Code:
tempTotal = parseInt(myform.elements[x].value)

Then in your loop, try combining the + and = together - it is the same as saying: add tempTotal to theTotal.

Code:
theTotal += tempTotal;

That should take care of things for you!

Jeff
 
You need to multiply each "value" by 1 before adding. Here's a modified version of your code - it writes the answer to a SPAN instead of the opening page... but you get the idea.
Code:
<script>
function fillValue() {
//	window.opener.document.forms[0].elements[1].value = ""
	var tempTotal, theTotal=0;
	var elemLen = myform.length;
	for (var x=0;x<elemLen;x++) {
		tempTotal = myform.elements[x].value*1;
		if (tempTotal > 0) {
			theTotal+=tempTotal;
		}
	}
//	window.opener.document.forms[0].elements[1].value = theTotal;
	document.getElementById('answer').innerHTML = theTotal;
	return false;
}
</script>

<form name="myform">
<input type="text" value="1">
<input type="text" value="2">
<input type="text" value="3">
<input type="text" value="4">
<input type="text" value="5">
<input type="text" value="6">
<input type="submit" onclick="return fillValue();">
</form>
Total is:<span id="answer">asdf</span>

Hope this helps.

Pete.

Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Good point BabyJeff - I forgot to "validate" the numbers first. [thumbsup2]

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Thanks guys!

yeah, I just used

Code:
parseInt()

and now it works.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top