0

I have a CheckBox list and I want to get the number of items selected when a checkbox is checked/unchecked.

checkCount is always 0. It seems to me like checkCount is getting assigned when the page loads and the getCheckCount function is never called again. Why doesn't this work and how can I get the event to have an up to date checkCount? Thank you.

   $(function () {
        $('#<%=cblInterests.ClientID%> input').click(function () {
            var checkCount = getCheckCount();
            alert(checkCount);
            return checkCount < 2;
        });
    });        

    function getCheckCount() {
        return $('#<%=cblInterests.ClientID%>').children('input:checked').length;
    }
1
  • Are the checkboxes directly beneath #client-id? If not, use "find" instead of "children" Commented Sep 17, 2011 at 20:58

2 Answers 2

0

Use the .change() event, not the .click() event.

Sign up to request clarification or add additional context in comments.

3 Comments

Same issue, the count is still always 0.
It would help if you could show your markup. But, try changing .children(...) to .find(...).
You're welcome. Next time, please post the relevant HTML as well.
0
   $(function () {
        $('#<%=cblInterests.ClientID%>').delegate('input', 'change', function () {
            var checkCount = getCheckCount();
            alert(checkCount);
            return checkCount < 2;
        });
    });        

    function getCheckCount() {
        return $('#<%=cblInterests.ClientID%> input:checked').length;
    }

the use of .delegate (or .live) allows you to add inputs on the fly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.