swgdesign
asked on
Limiting the number of checked checkboxes
I have the following code in Javascript that alerts me when 4 or more checkboxes have been checked, but what I would like it to do is to not allow any more checked checkboxes after 4 have been checked and to disable the checkboxlist/submit button etc. My code does not disable these elements :(
Can this be done from code behind???
Can this be done from code behind???
<asp:Content ID="Content1" ContentPlaceHolderID="Head" Runat="Server">
<script type="text/javascript" language="javascript">
function CheckCheck() {
var chkBoxList = document.getElementById('<%=cblTopics.ClientID %>');
var chkBoxCount = chkBoxList.getElementsByTagName("input");
var btn = document.getElementById('<%=btnSchedule.ClientID %>');
var i = 0;
var tot = 0;
for (i = 0; i < chkBoxCount.length; i++) {
if (chkBoxCount[i].checked) {
tot = tot + 1;
}
}
if (tot >= 4) {
alert('You may select up to 4 topics per meeting.');
btn.disabled = true;
}
else {
btn.disabled = false;
}
}
</script>
</asp:Content>
ASKER
I am happy with either a JS or VB solution, but it had to stop the user form selecting more than 4 checkboxes and alert them to the fact.
The checkboxlist is dynamically populated, so just use a static list of 6 checkboxes. It doesn't matter what their values or labels are as it is the functionality I need. :-)
THanks
The checkboxlist is dynamically populated, so just use a static list of 6 checkboxes. It doesn't matter what their values or labels are as it is the functionality I need. :-)
THanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cleaner function and does exactly what I wanted just a different way round! Thanks!
Where exactly in code behind you are planning to use this code?
Can you post cblTopics markup?
In the mean time check:
http://stackoverflow.com/questions/3770874/how-to-return-selected-items-from-checkboxlist-control-in-asp-net
Basically your code loops each item in cblTopics.Items property and check item.Selected property . item.Selected is equivalent to JS
Open in new window