Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Need to total textboxes
Old 08-06-2004, 08:37 PM Need to total textboxes
witchblade32's Avatar
Super Talker

Posts: 140
Location: Lititz, PA
Trades: 0
My javascript skills have always stunk. I have a bunch of textboxes that I need to total up and insert the value of into another onChange and I'm not sure where to begin.

I need to integrate this with a shopping cart so I don't have any choice in naming the fields.

Sample textboxes that need to be totalled are:
<input type="text" size="2" name="option|white|TDG11" >WHITE<br>
<input type="text" size="2" name="option|black|TDG11">BLACK<br>
<input type="text" size="2" name="option|lt grey|TDG11">LT GREY<br>

(they're product colors and the text box indicates how many of each color).

The box that needs to get the total of all the other boxes would look like:
<input type="text" name="item-TDG11|1:3.25,10:2.95,50:2.45|Gutermann Thread-1100 yards|-|-|" size=4>

If I can't get the total of all the option boxes into the main qty box, it won't calculate the qty discount correctly. Ideally, that qty box would be editable, but it has to be a form field to pass to the cart.

Any help, guidance or code samples would be greatly appreciated. My normal programmer dude is having roomate problems and couldn't help me and I'm on a time crunch to get this done.

Deb
__________________
When you eliminate all other possibilities, what remains, no matter how improbable, is the answer.
witchblade32 is offline
Reply With Quote
View Public Profile Visit witchblade32's homepage!
 
 
Register now for full access!
Old 08-07-2004, 06:06 AM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
ok, first of all, I reccomend giving all fields an ID; this won't affect how the form data is posted, don't worry! Use something like this;

HTML Code:
<script language="Javascript">
function changeTotal() {
	if(document.getElementById) {
		if(!parseInt(document.getElementById("o_w_tdg11").value)) { document.getElementById("o_w_tdg11").value="0"; }
		if(!parseInt(document.getElementById("o_b_tdg11").value)) { document.getElementById("o_b_tdg11").value="0"; }
		if(!parseInt(document.getElementById("o_l_tdg11").value)) { document.getElementById("o_l_tdg11").value="0"; }
		
		nTotal = 0;
		nTotal += parseInt(document.getElementById("o_w_tdg11").value);
		nTotal += parseInt(document.getElementById("o_b_tdg11").value);
		nTotal += parseInt(document.getElementById("o_l_tdg11").value);
		document.getElementById("t_tdg11").value = nTotal;
	} else if(document.all) {
		if(!parseInt(document.all['o_w_tdg11'].value)) { document.all['o_w_tdg11'].value="0"; }
		if(!parseInt(document.all['o_b_tdg11'].value)) { document.all['o_b_tdg11'].value="0"; }
		if(!parseInt(document.all['o_l_tdg11'].value)) { document.all['o_l_tdg11'].value="0"; }

		nTotal = 0;
		nTotal += parseInt(document.all['o_w_tdg11'].value);
		nTotal += parseInt(document.all['o_b_tdg11'].value);
		nTotal += parseInt(document.all['o_l_tdg11'].value);
		document.all['t_tdg11'].value = nTotal;
	}
	return;
}
</script>

<input id="o_w_tdg11" value="0" onchange='changeTotal();' type="text" size="2" name="option|white|TDG11" >WHITE<br>
<input id="o_b_tdg11" value="0" onchange='changeTotal();' type="text" size="2" name="option|black|TDG11">BLACK<br>
<input id="o_l_tdg11" value="0" onchange='changeTotal();' type="text" size="2" name="option|lt grey|TDG11">LT GREY<br>

<input id="t_tdg11" type="text" name="item-TDG11|1:3.25,10:2.95,50:2.45|Gutermann Thread-1100 yards|-|-|" size=4>
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE

Last edited by webwoRRks; 08-07-2004 at 06:08 AM.. Reason: I made it way too confusing
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-07-2004, 08:44 AM
witchblade32's Avatar
Super Talker

Posts: 140
Location: Lititz, PA
Trades: 0
Is there a cleaner way to do this with an array? Some of the products have 50 options and 3 or more products per page (which all need to be calculated separately). That's going to add significant bulk to the page.
__________________
When you eliminate all other possibilities, what remains, no matter how improbable, is the answer.
witchblade32 is offline
Reply With Quote
View Public Profile Visit witchblade32's homepage!
 
Old 08-07-2004, 06:01 PM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
An array of id's ? If thats what you mean, here is an example;

HTML Code:
<script language="Javascript">
allIDs = new Array('o_w_tdg11','o_b_tdg11','o_l_tdg11');
function changeTotal() {
	if(document.getElementById) {
		for(i=0;i<allIDs.length;i++) { if(!parseInt(document.getElementById(allIDs[i]).value)) { document.getElementById(allIDs[i]).value="0"; } }

		nTotal = 0;
		for(i=0;i<allIDs.length;i++) { nTotal += parseInt(document.getElementById(allIDs[i]).value); }

		document.getElementById("t_tdg11").value = nTotal;
	} else if(document.all) {
		for(i=0;i<allIDs.length;i++) { if(!parseInt(document.all[allIDs[i]].value)) { document.all[allIDs[i]].value="0"; } }

		nTotal = 0;
		for(i=0;i<allIDs.length;i++) { nTotal += parseInt(document.all[allIDs[i]].value); }
		document.all['t_tdg11'].value = nTotal;
	}
	return;
}
</script>

<input id="o_w_tdg11" value="0" onchange='changeTotal();' type="text" size="2" name="option|white|TDG11" >WHITE<br>
<input id="o_b_tdg11" value="0" onchange='changeTotal();' type="text" size="2" name="option|black|TDG11">BLACK<br>
<input id="o_l_tdg11" value="0" onchange='changeTotal();' type="text" size="2" name="option|lt grey|TDG11">LT GREY<br>

<input id="t_tdg11" type="text" name="item-TDG11|1:3.25,10:2.95,50:2.45|Gutermann Thread-1100 yards|-|-|" size=4>
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-08-2004, 10:09 AM
witchblade32's Avatar
Super Talker

Posts: 140
Location: Lititz, PA
Trades: 0
Thanks for your help.
__________________
When you eliminate all other possibilities, what remains, no matter how improbable, is the answer.
witchblade32 is offline
Reply With Quote
View Public Profile Visit witchblade32's homepage!
 
Reply     « Reply to Need to total textboxes
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22949 seconds with 12 queries