Home >>Javascript Programs >Print value of all checked (selected) Checkboxes on Button click using JavaScript

Print value of all checked (selected) Checkboxes on Button click using JavaScript

JavaScript - Print value of all checked (selected) Checkboxes on Button click

This article will help you how to write a JavaScript to print value of all checked or selected checkboxes on just clicking the button.

Sometimes, programmers are facing the problem that how to code by JavaScript to print all selected checkboxes print. So, in this article we help you how to write code to solve this problem.

Let's take an Example:

<html>
	<head>
		<title>Print value of all checked CheckBoxes on Button click.</title>
		<script type="text/javascript">
			function printChecked(){
				var items=document.getElementsByName('check_print');
				var selectedItems="";
				for(var i=0; i<items.length; i++){
					if(items[i].type=='checkbox' && items[i].checked==true)
						selectedItems+=items[i].value+"\n";
				}
				alert(selectedItems);
			}			
		</script>
	</head>
<body>
	<big>Select your favourite accessories: </big><br>
	<input type="checkbox" name="check_print" value="red">red<br>
	<input type="checkbox" name="check_print" value="Blue">Blue<br>
	<input type="checkbox" name="check_print" value="Green">Green<br>
	<input type="checkbox" name="check_print" value="Yellow">Yellow<br>
	<input type="checkbox" name="check_print" value="Orange">Orange<br>
	<p><input type="button" onclick='printChecked()' value="Click me"/></p>
</body>
</html>
Output:

Select your favourite accessories:
red
Blue
Green
Yellow
Orange