For Each Loop

Example

In this example, we use JavaScript to loop through all the checkboxes and find which are selected. Note: for in loop is better used for associative arrays, but we'll make it work here too

Make some selections

Pick some trees

Code:

<script>
 function getChecked(){
	var selections = new Array();
	var x;
	var checkBoxes = document.getElementsByName("trees");
	for(x in checkBoxes){
		if(checkBoxes[x].checked){
			selections.push(checkBoxes[x].value);
		}
	} 
	//output the checked values
	var el = document.getElementById("demo");
	el.innerHTML = selections.toString();	
}
</script>

Output: