Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

auto calculate dynamic checkboxes

Posted on 2009-02-18
13
Medium Priority
?
819 Views
Last Modified: 2012-05-06
In my ASP form named edcourses, I have a dynamic list of checkboxes of courses offered. The value for each contains coursename_price.

Am I able to have a TOTAL field that can be auto updated as each checkbox is checked/unchecked since I have the price in the checkbox value?

Thanks
...
do until rs.eof
 
<input type="checkbox" name="courselist" value="<%= rs("combo")%>" ><%= rs("coursename") & " $" & rs("price")%><br>
 
rs.MoveNext
Loop 
...

Open in new window

0
Comment
Question by:jcuWM
13 Comments
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 23673581
You can do this pretty easily with Javascript.  I was going to write this out, but there's already a pretty good example of how it's done right here:

http://www.mredkj.com/javascript/orderform.html

Hope that helps!
0
 

Author Comment

by:jcuWM
ID: 23673926
Tomeeboy,

Thanks for the quick reply. I used their Example 2 which worked but they are hard coding their checkboxes like this:

<input type="checkbox" id="chk0" />
Wonder Widget
$<span id="txtPrice0">10</span>
</p>
<p>
<input type="checkbox" id="chk1" />
Snow Widget
$<span id="txtPrice1">20</span>
<input type="text" id="txtTotal" size="8" />

and mine will be done dynamically so I won't know how many will be showing up. If I knew how many checkboxes I had, this would cut it.

In addition, I currently do a request.form("courselist") to get the list of checkboxes that were checked. I'm not familiar with what I would do using this person's solution to get that checkbox information.

I am not asking you to delve in to someone elses code either. Do you have available what you were going to supply?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 23692848
Something like this perhaps?


//----------------------------------------------------------------------
// Name: CBsum()
// Role: The parm specifies the name attribute of the CheckBox input
//       fields to be added
//----------------------------------------------------------------------
function CBsum( name ) {
  var result = 0.0
  var CBs = document.getElementsByName( name )
  for ( var i = 0; i < CBs.length; i++ ) {
    if ( CBs[ i ].checked ) {
      result += parseFloat( CBs[ i ].value )
    }
  }
  alert( 'Sum of selected checkboxes = ' + result )
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 41

Expert Comment

by:HonorGod
ID: 23692863
Then, all you have to do is have a button to invoke the function.  Something like this, for example:


<input type='button' value='Compute sum' onclick='CBsum("courselist")'> 

Open in new window

0
 

Author Comment

by:jcuWM
ID: 23737323
If I don't want to have a button clicked to get the total, could I make it a onChange and put that in the checkbox?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 23737392
Certainly.  Just have the onclick for the radiobutton input field call the function...
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 1200 total points
ID: 23737428
e.g.,
<input type='checkbox' onclick='CBsum("courselist")'>Compute sum<br/>

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 23737451
However, if you don't want the function called if the checkbox is unchecked, that would need to be slightly different
<input type='checkbox' onclick='if (this.checked) CBsum("courselist")'>Compute sum<br/>

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 23737460
Does that make sense?
0
 

Author Comment

by:jcuWM
ID: 23738681
HonorGod,

That makes sense. Thanks for the replies.
I'm running it even when they uncheck a previously checked checkbox so it will recalculate the total.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 23740429
Super.  I'm glad to have been able to help.

Thanks for the grade & points (you put me over 1,100,000) :-)

Good luck & have a great day
0
 

Expert Comment

by:hrooster
ID: 25339237
I am trying to modify this for my use but I am having trouble.  I have a preview page that groups individual bills under thier common parent invoice number.  I would like to use the check box to select which line item I might want to short pay and have it calculate each subtotal.
sample.txt
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 25352059
open a "related" question.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question