The values of the checkboxes are not getting stored. When i alert the values in place of selected += elements[i].value + "\n"; i am getting the values.
<script>
function whichIsChecked(){
var selected = "";
var elements = document.getElementsByName("colors[]");
for(var i=0; i<=elements.length; i++){
if(elements[i].checked){
//alert(elements[i].value) // values are displayed
selected += elements[i].value + "\n";
}
}
alert(selected); // not storing
}
</script>
<form method="post" action="">
<input type="checkbox" name="colors[]" value="red">
<input type="checkbox" name="colors[]" value="blue">
<input type="checkbox" name="colors[]" value="green">
<input type="button" name="submit" value="Submit" onclick="whichIsChecked()">
</form>