Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Created June 13, 2016 03:20
Show Gist options
  • Save Alex1990/d39327765e9f50c2ce95de5ae51f0c15 to your computer and use it in GitHub Desktop.
Save Alex1990/d39327765e9f50c2ce95de5ae51f0c15 to your computer and use it in GitHub Desktop.
Get the value of the checked checkbox or radio.
/**
* Get the value of the checked checkbox(s) or radio.
* @param {Element} inputs the input:checkbox or input:radio elements
* @return {String|Array} the checkec value
*/
function getCheckedValue(inputs) {
var val;
var len = inputs.length;
if (inputs[0].type === 'checkbox') {
val = [];
for (var i = 0; i < len; i++) {
if (inputs[i].checked) val.push(inputs[i].value);
}
} else {
val = null;
for (var i = 0; i < len; i++) {
if (inputs[i].checked) {
val = inputs[i].value;
break;
}
}
}
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment