Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

[solved]javascript checkboxes


  • Please log in to reply

#1
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 31,671 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
  • 31,671 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