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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with my addition! 1

Status
Not open for further replies.

andycheese

Programmer
Jun 10, 2002
38
GB
Hi, I have a page with 7 textboxes (corresponding to days of the week), into which the user can enter a value. I'm wanting a script that is called whenever the user changes a value in one of these boxes (so, presumably onBlur???) and adds up the total of all these boxes, then places it into another box (called TOTAL). I've had a go at doing this, but my code doesn't work at all! If anyone could give me any suggestions, I'd be most grateful.

function totalise()
{
var SUN = document.form1.SUN.value;
var MON = document.form1.MON.value;
var TUE = document.form1.TUE.value;
var WED = document.form1.WED.value;
var THU = document.form1.THU.value;
var FRI = document.form1.FRI.value;
var SAT = document.form1.SAT.value;
var TOTAL = SUN ++ MON ++ TUE ++ WED ++ THU ++ FRI ++ SAT;
document.form1.TOTAL.value == TOTAL;
}
 
Change:

var TOTAL = SUN ++ MON ++ TUE ++ WED ++ THU ++ FRI ++ SAT;
document.form1.TOTAL.value == TOTAL;

To:

var TOTAL = SUN+MON+TUE+WED+THU+FRI+SAT;
document.form1.TOTAL.value = TOTAL;

*Don't use Double plus(++) signs, use single. Don't use double egual (==) signs use single.



MrGreed

"did you just say Minkey?, yes that's what I said."
 
Can I suggest something more like this:
Code:
function totalise() {

	var total = 0; var the_form = document.forms[ "form1" ];
	var days = new Array( "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" );

	for( i=0; i < days.length; i++ ) {

		if( the_val = parseInt( the_form[ days [ i ] ].value ) ) {

			total += the_val;
		}
	}

	the_form.TOTAL.value = total;
}

HTH. :)
 
theboyhope - that's spot on! Excatly what I was trying to do, thank you very much. Now I've got to try and make it work with multiple rows :D (ie, 6 rows of 7 days, all with individual totals). Eeep!
 
Doh, shouldn't have spoken so soon ;)

Found a small problem. Tried changing the parseInt to parseFloat, and with some numbers it gives huge numbers after the decimal place. Any way I can limit the total to display in the format 9.2 (ie, 9 characted integer, 2 decimals?)

Sorry to keep asking!
 
Nine charactered integer with two decimal places? Sorry, what?

If you change the last line to
Code:
the_form.TOTAL.value = parseInt( total * 100 ) / 100;
does that do what you want?


 
Sorry, what I meant was, is there any way to stop a decimal number having .99999 recursive after the decimal place? And what I meant by 9.2, was a maximum value of 999999999.99, ie , 9 chars before the decimal, 2 after. Is that possible client side, or am I going into the realms of impossibility.
 
Erm, try this.
Code:
function totalise( evt ) {

	var total = 0; var the_form = document.forms[ &quot;form1&quot; ];
	var days = new Array( &quot;SUN&quot;, &quot;MON&quot;, &quot;TUE&quot;, &quot;WED&quot;, &quot;THU&quot;, &quot;FRI&quot;, &quot;SAT&quot; );

	for( i=0; i < days.length; i++ ) {

		if( the_val = parseFloat( the_form[ days [ i ] ].value ) ) {

			total += the_val;
		}
	}

	total = parseInt( total * 100 ) / 100;
	if( total > 999999999.99 || total < 0 ) {

		alert( &quot;Values are out of range.&quot; );
		evt = (evt) ? evt : ((window.event) ? event : null);
		var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
		elem.value = &quot;&quot;; elem.focus();

	} else {

		the_form.TOTAL.value = total;
	}
}

Needs tweaked a bit but it should get you going in the right direction. :)
 
Cheers! Any idea how I can adopt that multiple rows idea I mentioned earlier?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top