0

How can I get any checkbox from CheckBoxList is selected via jQuery?

Markup:

    <div>
        <asp:CheckBoxList ID="cblProduct" runat="server" CssClass="myProductCheckBoxList" TabIndex="14">
        </asp:CheckBoxList>
    </div>

i'ld like find any checked check boxes that has Cssclass-myProductCheckBoxList . (For validation of - checked-> atleast one product)

4 Answers 4

1

jQuery('.myProductCheckBoxList:checked').each(function(){

alert(jQuery(this).attr('checked'));

});

i dont know much of asp but i guess the checked filter will help you find all the checkboxes that are checked.

Actually the selector gives an array of all the selected elements. You can iterate over this as in code to read properties.

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

2 Comments

yes this gives some object. How can i make sure that something is selected or Not selected anything.
now you can iterate over this object to get all the selected elements modified code for iteration.
1

If you want to check at least 1 check box is selected, then you can try the following,

$(':checkbox.myProductCheckBoxList').is (':checked'); //returns true if at least 1 option is selected

You can try more using the jsFiddle Link here

2 Comments

this gives false all the time
Did you try to check 1 checkbox and validate? I tested it just now and it works fine.
1
jQuery('.myProductCheckBoxList').each(function() {
  if (jQuery(this).is(":checked")) {
    alert(jQuery(this).attr("value"));
  }
});

Translation: For each of the checkboxes with the class "myProductCheckBoxList", check if the current item is "checked", and if so, alert the curent checkboxes item.

var checked_product = false;
jQuery('.myProductCheckBoxList').each(function() {
  if (jQuery(this).is(":checked")) {
    checked_product = true;
  }
});
if (checked_product) {
  alert("one product is checked");
}

This checkes if one or more items are checked.

Comments

0
function SetProductCheckAll() {

    $('.myProductCheckBoxList :checkbox').click(function () {
        var toggle = this.checked;
        var value = this.value;
        var needCheckAll = true;
        if (value == "-1") {
            $('.myProductCheckBoxList :checkbox').attr("checked", toggle);
        }
        else {
            if (toggle == false) {
                $('.myProductCheckBoxList :checkbox').eq(0).attr("checked", false);
            }
            else {
                for (var count = 1; count <= $('.myProductCheckBoxList :checkbox').length; count = count + 1) {
                    if ($('.myProductCheckBoxList :checkbox').eq(count).attr("checked") == false) {
                        needCheckAll = false;
                    }
                }
                $('.myProductCheckBoxList :checkbox').eq(0).attr("checked", needCheckAll);
            }
        }
    });
}

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.