Unchecking two items in a checkboxlist?

G

Guest

Hi,
I need to tell a checkbox list that if two of the items have been checked,
to uncheck themselves. I need help! Thanks :)

~Chelimar
 
J

jongalloway

It sounds like you may want to switch to a RadioButtonList, then.
That's the default behavior of a radio button group (selecting one
deselects the previous).

Two benefits:
1. Less code for you to write, debug, and maintain
2. Standard HTML form behavior so your users will not be confused. A
checkbox list _should_ allow multiple selections according to the HTML
specs.

If you're stuck with this due to business requirements (I've been
there, too), you'll have to do this with Javascript on the client.
Here's some sample code to get you started (needs to be converted to a
webform):

<html>
<head>
<script>
function VerifyCheckboxes()
{
count = document.frm1.elements.length;
checkedElements = 0;
for (i=0; i < count; i++)
{
if(document.frm1.elements.checked == 1)
{
checkedElements++;
if(checkedElements>1)
{
uncheckAll();
}
}
}
}

function uncheckAll()
{
for (i=0; i < count; i++)
{
document.frm1.elements.checked = 0;
}
}
</script>
</head>
<body>
<form name="frm1">Sample
<input type="checkbox" onclick="VerifyCheckboxes()" value="1"/>
<input type="checkbox" onclick="VerifyCheckboxes()" value="2"/>
<input type="checkbox" onclick="VerifyCheckboxes()" value="3"/>
<input type="checkbox" onclick="VerifyCheckboxes()" value="4"/>
<input type="checkbox" onclick="VerifyCheckboxes()" value="5"/>
</form>
</body>
</html>
 
S

Saravana

Write one method which does this work and attach it to onclicked
eventhandler of both the checkbox.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top