Solved

How to calculate checkbox and form fields using javascript?

Posted on 2004-04-09
4
405 Views
Last Modified: 2013-12-16
I need a javascript function to do the following:

1.      when checkbox clicked, enable the text field associated with the checkbox
2.      sum values of checked items
3.      return that value to “total”
4.      if value is changed, recalculate

I can’t seem to figure it out.  And I’m not a javascript programmer.

Here's a sample of the form fields:

<input type=”checkbox” name=”chkbox” value=”1” onClick=”calc(this.form)”>
<input type=”text” name=”price1” value=”20” disabled>
<br>
<input type=”checkbox” name=”chkbox” value=”2” onClick=”calc(this.form)”>
<input type=”text” name=”price2” value=”30” disabled >
<br>
<input type=”checkbox” name=”chkbox” value=”n” onClick=”calc(this.form)”>
<input type=”text” name=”pricen” value=”50” disabled>
…etc

<input type=”text” name=”total” value=””>

Thanks
0
Comment
Question by:PhantaC
  • 2
4 Comments
 
LVL 2

Expert Comment

by:taherzm
Comment Utility
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
Comment Utility
Here you are.  Plus ditch whatever you used to write the code you posted and use an editor that puts in valid quote characters; like note pad.

<html>
<head>
<title> Calculate</title>
<script type="text/javascript">
<!--
function calc(frm)
{
   var thesum=0;
   for (i=0;i<frm.chkbox.length;i++)
   {
      str='price'+frm.chkbox[i].value;
      if (frm.chkbox[i].checked)
      {
         frm.elements[str].disabled=false;
         thesum+=parseInt(frm.elements[str].value);
      }
      else
      {
         frm.elements[str].disabled=true;
      }
   }
   frm.total.value=thesum;
}
//-->
</script>
</head>
<body>
<form>        
<input type="checkbox" name="chkbox" value="1" onClick="calc(this.form)">
<input type="text" name="price1" value="20" disabled>
<br>
<input type="checkbox" name="chkbox" value="2" onClick="calc(this.form)">
<input type="text" name="price2" value="30" disabled >
<br>
<input type="checkbox" name="chkbox" value="n" onClick="calc(this.form)">
<input type="text" name="pricen" value="50" disabled>
<input type="text" name="total" value="">
</form>
</body>
</html>
0
 

Author Comment

by:PhantaC
Comment Utility
awesome.  this place rocks!

about the HTML editor, I actually use HomeSite but I had the code written in an email to a friend and I just copied the email which made the quotes all funky.  

thanks though.  
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
Ah yes the email was probably using weird encoding.  When I first tried to test the page it threw a bunch of errors for illegal characters.  Homesite is fine.  Glad I could help. Thanks for the A. :^)

Cd&
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now