Thursday, July 5, 2012

Asp.Net jQuery checkboxlist get checked items list


In this post Asp.Net jQuery checkboxlist get checked items list, we shall see on how to loop through all the checkboxes in a CheckBoxList and get the list of values checked.

We can loop through all the checkboxes in the CheckBoxList by filtering the type checkbox in as follows

    var selectedIDs = '';
    var selectedValues = '';
    $('#chkListCity input:checkbox').each(function() {
        if ($(this).attr('checked')) {
            if (selectedIDs.length == 0) {
                selectedIDs = $(this).attr('id');
                selectedValues = $('label[for=' + this.id + ']').html();
            }
            else {
                selectedIDs += ", " + $(this).attr('id');
                selectedValues += ", " + $('label[for=' + this.id + ']').html();
            }
        }
    });
    alert('Selected IDs: ' + selectedIDs);
    alert('Selected Values: ' + selectedValues);

To loop through all the checkboxes in the page on the click event of a button use the following script.

// Loop through - Get List of Selected Checkboxes in the CheckBoxList
$('#cmdGetSelectedCheckboxList').click(function(event) {
    event.preventDefault();
    var selectedIDs = '';
    var selectedValues = '';
    $('#chkListCity input:checkbox').each(function() {
        if ($(this).attr('checked')) {
            if (selectedIDs.length == 0) {
                selectedIDs = $(this).attr('id');
                selectedValues = $('label[for=' + this.id + ']').html();
            }
            else {
                selectedIDs += ", " + $(this).attr('id');
                selectedValues += ", " + $('label[for=' + this.id + ']').html();
            }
        }
    });
    alert('Selected IDs: ' + selectedIDs);
    alert('Selected Values: ' + selectedValues);
});


To see a full example using jQuery and Checkbox refer to the post Asp.Net jQuery checkbox Tutorial
.





Related Posts

Search Flipkart Products:
Flipkart.com

No comments: