Home » Javascript

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

This JavaScript function will print value of all selected (checked) CheckBoxes, value will be printed in alert (message) box on Button Click event.

JavaScript Code Snippet - Print Value of All Checked (Selected) CheckBoxes

JavaScript function

<script type="text/javascript">
	function printChecked(){
		var items=document.getElementsByName('acs');
		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>

HTML Source Code with JavaScript

<!--JavaScript - Print value of all checked (selected) CheckBoxes on Button click.-->
<html>
	<head>
		<title>JavaScript - Print value of all checked (selected) CheckBoxes on Button click.</title>
		<script type="text/javascript">
			function printChecked(){
				var items=document.getElementsByName('acs');
				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 style="text-align: left;">
	<h1>JavaScript - Print value of all checked (selected) CheckBoxes on Button click.</h1>
	<big>Select your favourite accessories: </big><br>
	<input type="checkbox" name="acs" value="Mobile">Mobile<br>
	<input type="checkbox" name="acs" value="Tab">Tab<br>
	<input type="checkbox" name="acs" value="Laptop">Laptop<br>
	<input type="checkbox" name="acs" value="IPod">IPod<br>
	<p>
		<input type="button" onclick='printChecked()' value="Print Selected Items"/>
	</p>
</body>

</html>

Result

print value of all selected checkboxes, print value of all checked checkboxes





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.