Created
June 13, 2016 03:20
-
-
Save Alex1990/d39327765e9f50c2ce95de5ae51f0c15 to your computer and use it in GitHub Desktop.
Get the value of the checked checkbox or radio.
This file contains hidden or 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
/** | |
* 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