Sunday, November 27, 2016

jQuery: how to check any radio button in a group

Need to check a radio button in a group, i.e., where multiple buttons share the same name? Here's how.

To check the first button:
$('input[name=priority]:radio').each(function() {
  $(this).prop('checked', true);
  return false;
});

To check the nth button just track the index:
var match = 3;
$('input[name=priority]:radio').each(function(index) {
  if ( index == match ) {
    $(this).prop('checked', true);
    return false;
  }
});

0 Comments:

Post a Comment

<< Home