Thursday, July 5, 2012

Asp.Net jQuery checkbox get checked items list


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

We can loop through all the checkboxes in the page by filtering the type checkbox in the page elements as follows and get the list of checked values.

    var selectedIDs = '';
    var selectedValues = '';
    $("input[type=checkbox][checked]").each(function() {
        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 Page
$('#cmdGetSelectedList').click(function(event) {
    event.preventDefault();
    var selectedIDs = '';
    var selectedValues = '';
    $("input[type=checkbox][checked]").each(function() {
        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);
});


Search Flipkart Products:
Flipkart.com

No comments: