Tuesday, July 10, 2012

Asp.Net jQuery get List of checked RadioButtons in the Page


In this post Asp.Net jQuery get List of checked RadioButtons in the Page, we shall see on how to get a comma separated list of id’s of all the checked radio buttons in a page
We can use the following script to get the radio button list in a Page



 var selectedIDs = '';
 var selectedValues = '';
 $("input[type=radio][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);

On click of a button uses the following script.

// Loop through - Get List of Selected RadioButtons in the Page
$('#cmdGetSelectedList').click(function(event) {
    event.preventDefault();
    var selectedIDs = '';
    var selectedValues = '';
    $("input[type=radio][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: