I have a form. It has several products. Each product has a price listed. Each product can be selected via a checkbox. What I would like to do is have the sum of each selected product, added in total at the bottom of the form. I would prefer to use javascript/dhtml for this, I don't want anything complex, just something to add to the page and have it display the total live as the customer checks/unchecks a product. All I can find even remotely similar is this, http://javascript.internet.com/forms/confirm-order.html It doesn't do what I want, plus it doesn't work with my form. Just found this, not sure it will help, http://www.mcfedries.com/JavaScript/OrderTotals.asp
Start out with an array that has the price for each checkbox. Each checkbox's onclick method calls a function with the checkbox's id and an index into that array. The function checks to see whether the checkbox just got en-/dis-abled and adds/subtracts from the total price appropriately. It can be tricky to get right, but isn't everything with javascript? It gets nastier if you're generating the page dynamically (and you will be doing that, won't you?). When I first started learning ASP.NET, I tried to do this. It turned into yet another one of those "If MS didn't already provide a control for you to do this, just forget it" things.
It's just for a basic html page. I definitley have a headache from trying to figure this out today, I don't know anything about Javascript, was just hoping someone knew or can point me in the right direction.
I found this on it, but the script will get rather long once I add all my products, better than nothing I suppose. For anyone else who needs it, http://www.madirish.net/tech.php?section=1&article=7
I'm not sure if this is what you need, but maybe it will help... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head><title>Untitled</title></head> <body> <form> <input onclick="clickCh(this)" type="checkbox" name="one" value="10"> $10.00<br> <input onclick="clickCh(this)" type="checkbox" name="two" value="12"> $12.00<br> <br> <input id="total" type="text" name="total"> </form> <script language="JavaScript" type="text/javascript"> var total = document.getElementById("total") function clickCh(caller){ if(caller.checked){ add(caller) } else { subtract(caller) } } function add(caller){ total.value = total.value*1 + caller.value*1} function subtract(caller){ total.value = total.value*1 - caller.value*1} </script> </body> </html>
Thanks marty you have saved much of my time. Your code is perfect after some modification with my code. Thanks bro.