Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 5th, 2006, 05:40 PM
Registered User
 
Join Date: Mar 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to get the sum of selected checkboxes?

Hello, I am new here and have a question. :)

I need to do something similar to the solution that was offered here, except for checkboxes and have the results display dynamically right on the page. http://p2p.wrox.com/topic.asp?TOPIC_ID=34924

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.

 
Old March 6th, 2006, 01:57 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Colleen!!

if every checkbox has different name then
use
if(document.formname.checkboxname.checked)
 {
// get the value of checkbox ,to calculate price
}

if you are using checkbox group then use
for(i=0;i<document.formname.checkboxname.length;i+ +)
{
if(document.formname.checkboxname[i].checked)
 {
// get the value of checkbox ,to calculate price
 }
}

hope this will help you


Cheers :)

vinod
 
Old March 6th, 2006, 09:08 AM
Registered User
 
Join Date: Mar 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks vinod,

I am using the same name for them all. I found this site, http://www.madirish.net/tech.php?section=1&article=7 and it helped me do what I want, aside from the fact that the script is a bit long once you start adding more items to it.

Thanks again!
Colleen

 
Old March 6th, 2006, 09:44 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Colleen!!

Plz try the code ,Hope this will help you
<script>
function getSum()
{
totalvalue=0
obj=document.myform.chkbox
for(i=0;i<obj.length;i++)
{
if(obj[i].checked)
{
totalvalue+=parseInt(obj[i].value)
}

}

if(totalvalue==0)
{
alert("Please select atleast one product!!");
document.myform.totalsum.value=""
}
else
{
document.myform.totalsum.value=totalvalue
}

}

function checkAll(obj)
{
if(obj.checked)
{obj=document.myform.chkbox
for(i=0;i<obj.length;i++)
    {
    obj[i].checked=true
    }
}
else
{obj=document.myform.chkbox
 for(i=0;i<obj.length;i++)
    {
    obj[i].checked=false
    }
}

}
</script>
<form name="myform">
Check All Product<input type=checkbox name=chkall onclick="checkAll(this)" >
<br>
Product1 Price 12 <input type=checkbox name=chkbox value=12><br>

Product2 Price 13 <input type=checkbox name=chkbox value=13><br>
Product3 Price 14 <input type=checkbox name=chkbox value=14><br>
Product4 Price 15 <input type=checkbox name=chkbox value=15><br>
Product5 Price 16 <input type=checkbox name=chkbox value=16><br>
Product6 Price 17 <input type=checkbox name=chkbox value=17><br>
Product7 Price 18 <input type=checkbox name=chkbox value=18><br>

Total Product Price<input type=text name="totalsum" readonly>
<input type=button onclick="getSum()" value="Get Total Price">




Cheers :)

vinod
 
Old March 7th, 2006, 01:34 PM
Registered User
 
Join Date: Mar 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much Vinod! You've been very generous and helpful. :D






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Checkboxes in Word cbraun99 Word VBA 3 December 29th, 2006 03:57 AM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
syntax error on <option selected="selected"> hamid HTML Code Clinic 1 October 13th, 2004 09:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.