Skip to content

Instantly share code, notes, and snippets.

@turboMaCk
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save turboMaCk/08adc1c96b8f5728dab7 to your computer and use it in GitHub Desktop.

Select an option

Save turboMaCk/08adc1c96b8f5728dab7 to your computer and use it in GitHub Desktop.
radios
<!doctype html>
<input type="radio" name="aasd">
<input type="radio" name="aasd">
<input type="radio" name="aasd">
<input type="radio" name="aasd">
<input type="radio" name="aasd">
<input type="radio" name="aasd">
<br>
<input type="radio" name="aasd2">
<input type="radio" name="aasd2">
<input type="radio" name="aasd2">
<input type="radio" name="aasd2">
<input type="radio" name="aasd2">
<input type="radio" name="aasd2">
<script>
function groupRadios() {
// ram eaters
var result = [];
var radios = document.querySelectorAll('input[type=radio]');
var name;
// loop all radios
[].forEach.call(radios, function(radio) {
// cache name of current
name = radio.getAttribute('name');
// if nothing is store under this name create new group
if (result[name] == undefined) {
result[name] = [];
}
// add radio to its goup
result[name].push(radio);
});
// return of the Jedi
return result;
}
// call and log
var radioGroups = groupRadios();
console.log(radioGroups);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment