Skip to content

Instantly share code, notes, and snippets.

@bcls
Created December 23, 2017 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcls/dd8aad189a3d9c8c7599aa5a24160a18 to your computer and use it in GitHub Desktop.
Save bcls/dd8aad189a3d9c8c7599aa5a24160a18 to your computer and use it in GitHub Desktop.
Get Array of Checked checkbox Values #javascript
// collection of checkboxes obtained by getElementsByName()
/**
* get array of values for checked boxes in a collection
* @param {htmlElementCollection} checkBoxCollection collection of checkbox elements
* @return {Array} array of the values of the checked boxes
*/
function getCheckedBoxValues(checkBoxCollection) {
var checkedValues = [],
i,
iMax;
if (checkBoxCollection) {
iMax = checkBoxCollection.length;
for (i = 0; i < iMax; i++) {
if (checkBoxCollection[i].check === true) {
checkedValues.push(checkBoxCollection[i].value);
}
}
return checkedValues;
} else {
console.log('Error: no input recieved');
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment