-
-
Save codingwithsara/645d4777f0d2fed49b94b1cc87593bd0 to your computer and use it in GitHub Desktop.
How to get the values of checked checkboxes in JavaScript
This file contains 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
<!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