I'm trying to make the button active if at least one checkbox is selected, but this only works for the last checkbox, for some reason the first one does not activate
function chose(){
var txt="";
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
console.log(checkboxes);
for(var i=0;i<checkboxes.length;i++){
if(checkboxes[i].checked){
document.getElementById("btn").disabled = false;
txt = txt + checkboxes[i].value + " ";
}
else{
document.getElementById("btn").disabled = true;
}
}
var result=document.getElementById("result");
result.innerHTML=txt;
}
<form action="" method="GET">
<label for="user">Name</label>
<input type="text" name="username" id="user" value="" placeholder="Input name">
<input type="button" name="" value="Send" id="btn" disabled="true" onclick="chose()">
IceCream:<input type="checkbox" name="one" id="param_1" value="IceCream" onchange="chose()">
Chocolate:<input type="checkbox" name="one" id="param_2" value="Chocolate" onchange="chose()">
</form>
Aucun commentaire:
Enregistrer un commentaire