Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Last active November 8, 2018 14:10
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 codingwithsara/645d4777f0d2fed49b94b1cc87593bd0 to your computer and use it in GitHub Desktop.
Save codingwithsara/645d4777f0d2fed49b94b1cc87593bd0 to your computer and use it in GitHub Desktop.
How to get the values of checked checkboxes in JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get Check Box Value</title>
<style>
.container {
margin: 100px 50px;
font-size: 20px;
}
</style>
</head>
<body>
<div class="container">
<input type="checkbox" class="checks" value="Apple"> A____<br>
<input type="checkbox" class="checks" value="Banana"> B_____<br>
<input type="checkbox" class="checks" value="Carrot"> C_____<br>
<br><br>
<a href="#" onclick="getValue();return false;">Get Value</a>
</div>
<script>
function getValue() {
var checks = document.getElementsByClassName('checks');
var str = '';
for ( i = 0; i < 3; i++) {
if ( checks[i].checked === true ) {
str += checks[i].value + " ";
}
}
alert(str);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment