2
I am trying to enable a button if at least one of the six checkbox is enabled, and disable any checkbox is enabled. I made a function, but it only enables and does not disable if I uncheck all checkboxes. Help me, please.
<html>
<head>
<title></title>
</head>
<body>
<div id= "consulta0" class="toggle div-inline" align="center">
<input type="checkbox" id="consultar-acervo" data-id="consultar-acervo" name="toggle" onclick="ativar()">
<p class= "nomeToogle"> Consultar Acervos</p>
</div>
<div id= "consulta1" class="toggle div-inline" align="center">
<input type="checkbox" id="consultar-acervo" data-id="consultar-acervo" name="toggle" onclick="ativar()">
<p class= "nomeToogle"> Consultar Acervos</p>
</div>
<div id= "consulta2" class="toggle div-inline" align="center">
<input type="checkbox" id="consultar-acervo" data-id="consultar-acervo" name="toggle" onclick="ativar()">
<p class= "nomeToogle"> Consultar Acervos</p>
</div>
<div id= "consulta3" class="toggle div-inline" align="center">
<input type="checkbox" id="consultar-acervo" data-id="consultar-acervo" name="toggle" onclick="ativar()">
<p class= "nomeToogle"> Consultar Acervos</p>
</div>
<div id= "consulta4" class="toggle div-inline" align="center">
<input type="checkbox" id="consultar-acervo" data-id="consultar-acervo" name="toggle" onclick="ativar()">
<p class= "nomeToogle"> Consultar Acervos</p>
</div>
<div id= "consulta5" class="toggle div-inline" align="center">
<input type="checkbox" id="consultar-acervo" data-id="consultar-acervo" name="toggle" onclick="ativar()">
<p class= "nomeToogle"> Consultar Acervos</p>
</div>
<div class=" item1">
<button type="button" class="btn btn-success" id="aplica" onclick="checar()" disabled> Aplicar</button>
</div>
</body>
</html>
Function
function ativar(){
var checa = document.getElementsByName("toggle");
var bt = document.getElementById("aplica");
var numElementos = checa.length;
var i = 0;
var cont =0;
for(i=0; i<= numElementos; ++i){
if(checa[i] !== undefined){
++cont;
}
}
if(cont >=1){
document.getElementById("aplica").removeAttribute("disabled");
}
else{
document.getElementById("aplica").setAttribute("disabled");
} }
Your page has jquery library?
– user60252