We help IT Professionals succeed at work.

auto calculate dynamic checkboxes

jcuWM
jcuWM asked
on
Medium Priority
861 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

Comment
Watch Question

CERTIFIED EXPERT

Commented:
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!

Author

Commented:
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?
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
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

HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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?
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
Certainly.  Just have the onclick for the radiobutton input field call the function...
Software Engineer
CERTIFIED EXPERT
Commented:
e.g.,
<input type='checkbox' onclick='CBsum("courselist")'>Compute sum<br/>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
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

HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
Does that make sense?

Author

Commented:
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.
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
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

Commented:
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
HonorGodSoftware Engineer
CERTIFIED EXPERT

Commented:
open a "related" question.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.