Skip to content

Instantly share code, notes, and snippets.

@ssuryar
Last active April 25, 2017 10:29
Show Gist options
  • Save ssuryar/3d93344913fa7c7d083e7ad5c470000c to your computer and use it in GitHub Desktop.
Save ssuryar/3d93344913fa7c7d083e7ad5c470000c to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
Checkbox:
<label for="UK">
<input type="checkbox" name="myCheck" value="UK" id="UK">United Kingdom
</label>
<label for="USA">
<input type="checkbox" name="myCheck" value="USA">United States
</label>
<label for="IL">
<input type="checkbox" name="myCheck" value="IL">Illinois
</label>
<label for="MA">
<input type="checkbox" name="myCheck" value="MA">Massachusetts
</label>
<label for="UT">
<input type="checkbox" name="myCheck" value="UT">Utah
</label>
<input type="button" value="Click" id="btntest" />
<script>
function myFunction() {
var selchbox = [];
var inputfields = document.getElementsByName("myCheck");
var ar_inputflds = inputfields.length;
for (var i = 0; i < ar_inputflds; i++) {
if (inputfields[i].type == 'checkbox' && inputfields[i].checked == true)
selchbox.push(inputfields[i].value);
}
return selchbox;
}
document.getElementById('btntest').onclick = function(){
var selchb = myFunction(); // gets the array returned by getSelectedChbox()
alert(selchb);
console.log(selchb);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment