View Full Version : i have problem regarding counting checkboxes

07-11-2006, 08:42 AM
HI everyone,

In my project i have group of 7 check boxes. Each check box is given a value(as 1,2,3,4,5 etc.). The sum of checkboxes should not be over some value(let us suppose 7). If the user checks on boxes, and if the value is greater than the predetermined value, windows alert has to be there telling that it exceeded the predetermined value.

can anyone help me in writing javascript for this??????:confused: It will be of great help to me


artman 186
07-11-2006, 09:29 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<script type="text/javascript">

var counter = 0;

function checkValue(o) {
if (o.checked == true) {
if (counter + parseInt(o.value) > 7) {
o.checked = false;
alert('Over 7');
else {
counter = counter + parseInt(o.value);
else {
counter -= parseInt(o.value);

document.getElementById('value').innerText = counter;




<input type="checkbox" value="1" onclick="checkValue(this)">1<br>
<input type="checkbox" value="2" onclick="checkValue(this)">2<br>
<input type="checkbox" value="3" onclick="checkValue(this)">3<br>
<input type="checkbox" value="4" onclick="checkValue(this)">4<br>
<input type="checkbox" value="5" onclick="checkValue(this)">5<br>
<input type="checkbox" value="6" onclick="checkValue(this)">6<br>
<input type="checkbox" value="7" onclick="checkValue(this)">7<br>

Value: <span id="value">0</span>



artman 186
07-11-2006, 09:32 PM
Whoops....for cross-browser friendliness, replace:

document.getElementById('value').innerText = counter;


document.getElementById('value').firstChild.nodeValue = counter;

07-12-2006, 12:15 AM
Thanks very much......., it works fine:) :) :). But, if the values of the checkboxes are decimal values(like .75, .25 etc....), can u please tell me how to do that

07-12-2006, 12:31 AM
yeah, i got it i changed parseInt to parseFloat!!!!! thanx

07-12-2006, 12:36 AM
Thanks for doing his homework assignment for him. Just remember in the end its his grade and when it comes to an exam he isn't going to pass.