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???

<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>

Open in new window

swgdesignAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
Do you require a tranlation from JS to VB.NET?
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
chkBoxCount[i].checked

Open in new window

0
swgdesignAuthor Commented:
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
0
Miguel OzSoftware EngineerCommented:
Your method can be simplified using Jquery checked selector as followed:
function CheckCheck() {
    var btn = document.getElementById('<%=btnSchedule.ClientID %>');
    var tot = $('#' + '<%=cblTopics.ClientID %>' + ' :checkbox:checked').length;
    if (tot >= 4) {
        alert('You may select up to 4 topics per meeting.');
        btn.disabled = true;
    }
    else {
        btn.disabled = false;
    }
}

Open in new window

Note: VS2012 ASP.net projects come with jquery 1.7.2 in the scripts folder.

If futher help is needed please answer:
What is wrong with your original JS code? (e.g. value of tot when 2 checks are present)
Where is this method CheckCheck called? Is this method being called at all?
Also please post tot and btn values from debugger.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
swgdesignAuthor Commented:
Cleaner function and does exactly what I wanted just a different way round! Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.