Skip to content

Instantly share code, notes, and snippets.

@trinvh2
Last active February 21, 2023 09:11
Show Gist options
  • Save trinvh2/6df5e322c904911b9dc5 to your computer and use it in GitHub Desktop.
Save trinvh2/6df5e322c904911b9dc5 to your computer and use it in GitHub Desktop.
Select random radio buttons
var groups = {};
$('input:radio').each(function() {
groups[this.name] = true;
});
$.each(groups, function(name, bool) {
var radios = $('input:radio[name=' + name + ']');
if(radios.length > 0) {
var rd = Math.floor(Math.random() * radios.length);
// Remove first and 2nd option
rd = ((rd + 2) < radios.length) ? rd + 2 : rd;
radios[rd].checked = true;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment