0

I'm trying to load up a hidden field with a list of values from all checkboxes that are checked. Everything works fine except that the is(':checked') always returns false. Any ideas?

            $('.invCheckBox').click(function () {
            var cb = '';
            var i = 0;
            debugger;
            $(".invCheckBox").each(function (index, element) {
                debugger;
                if ($(element).is(':checked')) {
                    alert($(element).attr('invNbr'));
                    if (i == 0) {
                        cb = $(element).attr('invNbr')
                    }
                    else {
                        cb = cb + ',' + $(element).attr('invNbr');
                    };
                    i = i + 1;

                };
            });
            $('MainContent_txtHiddenField').text(cb);
        });
3
  • Are you doing a for each element on every item checked? Commented Jan 12, 2014 at 20:30
  • A good guess would be that the checkbox isn't checked when you're running the code. Commented Jan 12, 2014 at 20:30
  • 1
    Also if using jquery with a checkbox rather use a change event and not a click event. Commented Jan 12, 2014 at 20:31

3 Answers 3

1

Here is a fiddle for you might help :) http://jsfiddle.net/sAyfw/

$('.invCheckBox').change(function () {

$('.invCheckBox').each(function()
                       {
                           alert($(this).is(':checked'));
                       });
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Larid, I copied your code directly and tested it in my project and all the checkboxes still come back as false. I should add that these checkboxes are added dynamically from server-side code. And I notice that the checkboxes are wrapped in a span tag. Not sure if that's normal behavior for ASP.Net (<span class="invCheckBox" invnbr="1"><input name="ctl00$MainContent$ctl12" type="checkbox"></span>).
The jquery selector is getting the span and not the checkbox the checkbox should have the class of "invCheckBox" to ensure it is selected. could you maybe show your .aspx code / htm markup. Or are you using razor?
You were on the right track, Laird! I figured it out. It had to do with the way I was creating the control server-side. I was creating a CheckBox control...I changed it to an HtmlInputCheckbox and it started working fine. I'll update my question to show the answer.
0

For whatever reason creating a CheckBox control server-side wraps the input tag in a span tag. Weird. So, I changed the code to create an HtmlInputCheckBox instead and now the jQuery works beautifully.

            Dim divCheckBox As New HtmlGenericControl("div")
        divCheckBox.Attributes.Add("style", "width: 95px; float: left;")
        Dim chkCheckBox As New HtmlInputCheckBox
        chkCheckBox.Attributes.Add("class", "invCheckBox")
        chkCheckBox.Attributes.Add("invNbr", invoice.InvoiceNbr)
        divCheckBox.Controls.Add(chkCheckBox)
        divInvRow.Controls.Add(divCheckBox)

Comments

0

Thanks for the info about asp:CheckBox being wrapped in a span. My jQuery was always returning false, and I couldn't figure out why it was not working.

I used this:

console.log($(this).children(1).is(':checked'));

and that works perfectly with an asp:CheckBox.

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.