Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

[solved]javascript checkboxes


  • Please log in to reply

#1
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 33,101 posts
Hi,

I found this nice script that "almost" does what I want:

<html>
<head>
<script language="JavaScript"><!--
function calculate(what) {
  what.answer.value =0;
	for (var i=1,answer=0;i<10;i++)
		answer += (what.elements['Checkbox' + i].checked)&(what.elements['Checkbox' + i].value-0);
	what.answer.value = answer;
}

//--></script>
</head>
<body>

<form ID="Form2">
<INPUT type="checkbox" ID="Checkbox1" NAME="Checkbox1" VALUE="1"
onclick="calculate(this.form)"><br>
<INPUT type="checkbox" ID="Checkbox2" NAME="Checkbox2" VALUE="1"
onclick="calculate(this.form)"><br>
<INPUT type="checkbox" ID="Checkbox3" NAME="Checkbox3" VALUE="1"
onclick="calculate(this.form)"><br>
<INPUT type="checkbox" ID="Checkbox4" NAME="Checkbox4" VALUE="1"
onclick="calculate(this.form)"><br>
<INPUT type="checkbox" ID="Checkbox5" NAME="Checkbox5" VALUE="1"
onclick="calculate(this.form)"><br>
<INPUT type="checkbox" ID="Checkbox6" NAME="Checkbox6" VALUE="1"
onclick="calculate(this.form)"><br>
<INPUT type="checkbox" ID="Checkbox7" NAME="Checkbox7" VALUE="1"
onclick="calculate(this.form)"><br>
<INPUT type="checkbox" ID="Checkbox8" NAME="Checkbox8" VALUE="1"
onclick="calculate(this.form)"><br>
<INPUT type="checkbox" ID="Checkbox9" NAME="Checkbox9" VALUE="1"
onclick="calculate(this.form)"><br>

<input type="text" name="answer" ID="Text13">
</body>
</form>


The problem is that I want to assign different values to the checkboxes ( 1,2,4,8,16,32 etc)
But as soon as I try that it stops working.
Can this thing only add up 1's ?
As usual I'm probably missing something obvious. :)

Thanks for your help.

Edited by Metallica, 28 February 2008 - 10:07 AM.
set to solved

  • 0

Advertisements


#2
Metallica

Metallica

    Spyware Veteran

  • Topic Starter
  • GeekU Moderator
  • 33,101 posts
Solved it myself by remodelling another script I found.

Now I have this and it works:

<html> 
 <head> 
  <meta http-equiv="content-type" content="text/html;charset=iso-8859-1"> 
  <title>Waarden optellen</title> 
  <script type="text/javascript"> 
   function sumup() { 
	var sum = 0; 
	for (var i = 0; i < document.f.field.length; i++) if (document.f.field[i].checked) {
	 sum += parseFloat(document.f.field[i].value); }
	 document.getElementById("sum").innerHTML = (isNaN(sum) ? 'Geen getal.' : sum);
	};
  </script> 
 </head> 
 <body onload="sumup()"> 
  <form name="f"> 
   Veld 1: <input type="checkbox" name="field" value="1" onclick="sumup();"><br>
   Veld 2: <input type="checkbox" name="field" value="2" onclick="sumup();"><br>
   Veld 3: <input type="checkbox" name="field" value="4" onclick="sumup();"><br>
   Veld 4: <input type="checkbox" name="field" value="8" onclick="sumup();"><br>
   Veld 5: <input type="checkbox" name="field" value="16" onclick="sumup();"><br>
   Veld 6: <input type="checkbox" name="field" value="32" onclick="sumup();"><br>
  </form> 
  Totaal: <span id="sum"></span> 
 </body> 
</html>

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP