Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

How to calculate checkbox and form fields using javascript?

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
PhantaC
Asked:
PhantaC
  • 2
1 Solution
 
taherzmCommented:
0
 
COBOLdinosaurCommented:
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
 
PhantaCAuthor Commented:
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
 
COBOLdinosaurCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now