Hi guys tell me how to select all checkbox in single checkbox button
Hi niravmehtak,
This can be done I'm sure. What you need to do is set the value of all the other checkboxes on the clik of a controlling button.
I would do this using document.getElementById('checkbox').innerHTML.
So, on the controlling button you set the onclick() event to call a javascript function., into this function you can pass the ID's of the checkboxes you wish to set, then in a loop you can set the value to what it is not. So:
[html]
<form name="checkboxtest" action="target.html" methos="post>
<input type="checkbox" id="checkbox1" value="test1" />
<input type="checkbox" id="checkbox2" value="test2" />
<input type="checkbox" id="checkbox3" value="test3" />
<input type="checkbox" id="checkbox4" value="test4" />
<input type="button" id="checkboxAll" value="select all" onclick="selectAll('checkbox1, checkbox2, checkbox3, checkbox4', 'checkboxAll' />
</form>
[/html]
Then the javascript would look something like
-
function selectAll(pcChekcBoxList, pcControllingItem)
-
{
-
var laItems = pcCehckBoxList.split(',')
-
// creates an array
-
lnArrayCntr = 0
-
lnArrayLength = laItems.length - 1
-
for(lnArrayCntr=0; lnArraycntr <= lnArrayLength; lnArrayCntr++)
-
{
-
if(document.getElementById(laItems[lnArrayCntr]).checked == "checked")
-
{
-
document.getElementById(laItems[lnArrayCntr]).checked = ""
-
}
-
else
-
{
-
document.getElementById(laItems[lnArrayCntr]).checked = "checked"
-
}
-
if(document.getElementById(pcControllingItem).value == "Select All")
-
{
-
document.getElementById(pcControllingItem).value = "Unselect All"
-
}
-
else
-
{
-
document.getElementById(pcControllingItem).value = "Select All"
-
}
-
}
-
}
-
I haven't tested this, but the theory is correct and it should work. You may need to add some extra twiddles to set the button correctly. You could add an onclick to each checkbox that counts how many checkeboxes are checked. If it's all of them then set the button to "Unselect All", if it's not all of them then ensure that the button says "Select All". For this I would also use an array and a for loop.
That's my suggestion, I hope it helps. If you go to the nth degree, as I mention here this should work well in all circumstances.
Cheers
nathj