Last active
February 21, 2023 09:11
-
-
Save trinvh2/6df5e322c904911b9dc5 to your computer and use it in GitHub Desktop.
Select random radio buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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