Jump to content

childnodes > checkboxes


phorcon3

Recommended Posts

<div id="scroll">
<div><input type="checkbox" name="test[]" value="0" onClick="clickBox(0);" /></div>
<div><input type="checkbox" name="test[]" value="1" onClick="clickBox(1);" /></div>
<div><input type="checkbox" name="test[]" value="2" onClick="clickBox(2);" /></div>
<div><input type="checkbox" name="test[]" value="3" onClick="clickBox(3);" /></div>
</div>

<div>You have <span id="count">0</span> checkboxes out of 3 selected.

 

so, what I wanna do is:

 

a) When a checkbox is selected, I want to update the <span id="count">0</span>

 

document.getElementById('count').innerHTML = total;

 

b) You can only select 3 out of the 4 checkboxes

 

c) When someone deselects a checkbox I, of course wanna update the <span id="count">0</span>

 

does anyone know how to do this?

 

 

Link to comment
Share on other sites

<script language="javascript">
var total="0";
function clickBox(what,which)
{
var whichone = document.getElementById(which).checked;
if (whichone == true)
{
total++;
}
else {
total--;
}
if (total <= 3) {
document.getElementById('count').innerHTML = total;
}
else {
alert("You May Only Select Three Of These");
document.getElementById(which).checked = false;
}
}
</script>

<div id="scroll">
<div><input type="checkbox" id="test1" value="1" onClick="clickBox(this.value,this.id);" /></div>
<div><input type="checkbox" id="test2" value="1" onClick="clickBox(this.value,this.id);" /></div>
<div><input type="checkbox" id="test3" value="1" onClick="clickBox(this.value,this.id);" /></div>
<div><input type="checkbox" id="test4" value="1" onClick="clickBox(this.value,this.id);" /></div>
</div>

<div>You have <span id="count">0</span> checkboxes out of 3 selected.

Link to comment
Share on other sites

UPDATE

 

Do it like this instead; I did not factor in something in the previous script.

 

<script language="javascript">
var total="0";
function clickBox(what,which)
{
var whichone = document.getElementById(which).checked;
if (whichone == true)
{
total++;
}
else {
total--;
}
if (total <= 3) {
document.getElementById('count').innerHTML = total;
}
else {
alert("You May Only Select Three Of These");
document.getElementById(which).checked = false;
total=total-1;
}
}
</script>

<div id="scroll">
<div><input type="checkbox" id="test1" value="1" onClick="clickBox(this.value,this.id);" /></div>
<div><input type="checkbox" id="test2" value="1" onClick="clickBox(this.value,this.id);" /></div>
<div><input type="checkbox" id="test3" value="1" onClick="clickBox(this.value,this.id);" /></div>
<div><input type="checkbox" id="test4" value="1" onClick="clickBox(this.value,this.id);" /></div>
</div>

<div>You have <span id="count">0</span> checkboxes out of 3 selected.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.