0
Good morning, I’m studying javascript and I came across a problem: I created a logic where I provide two buttons, one to free access that when clicked is on the screen only the check icon, and the block that when clicked is only the blocked icon. The problem is that the icons of the checked items can be changed, but even with both checked items the only one that changes the icon is the first one. I’m changing the icon letting your display "None" and "initial" I did this way because I don’t know any other way, if you know something with javascript thank you, but the question is how do I change the icon happen in all checked items and not just in the first ? Thank you.
JS:
function bloquearAcesso(){
let inputs = document.querySelectorAll(".checar");
for(let i = 0; i< inputs.length; i++){
if(inputs[i].checked == true){
document.getElementById("liberado").style.display = "none";
document.getElementById("bloqueado").style.display = "initial";
}
};
};
function liberarAcesso(){
let inputs = document.querySelectorAll(".checar");
for(let i = 0; i < inputs.length; i++){
if (inputs[i].checked == true){
document.getElementById("bloqueado").style.display = "none";
document.getElementById("liberado").style.display = "initial";
};
};
}
HTML:
<html>
<body>
<div class="box-body no-padding">
<table class="table table-striped">
<thead>
<tr>
<th>Patio</th>
<th>Acessar Desktop</th>
<th>Criado em</th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="checar" type="checkbox">Patio1</td>
<td> <i id="liberado" class="fa fa-check"></i> <i id="bloqueado" class="far fa-times-circle"></i></td>
<td>02/04/2018</td>
<td>
</td>
</tr>
<tr>
<td><input class="checar" type="checkbox">Patio2</td>
<td> <i id="liberado" class="fa fa-check"></i> <i id="bloqueado" class="far fa-times-circle"></i></td>
<td>02/04/2018</td>
</tr>
<br/>
</tbody>
</table>
<button onclick="liberarAcesso()" type="button" class="btn btn-primary btn-xs btn-flat">Liberar acesso</button>
<button onclick="bloquearAcesso()" type="button" class="btn btn-danger btn-xs btn-flat">Bloquear acesso</button>
</div>
<script src="./curso.js">
</script>
</body>
</html>
You cannot repeat id’s. Set the id "released" as a class. The same with the id "locked".
– Sam
In case I would use getElementByClassName ? I tried here and gave: It is not possible to set the Display property of Undefined. as there are two classes in the icon, I tried by name and gave the same error
– Israel Gomes
Can use
document.getElementByClassName("liberado")
ordocument.querySelectorAll(".liberado")
... then you have to make a loop to catch the element by the index. The set of elements of the same class is an array. To catch the first, for example, you would:document.getElementByClassName("liberado")[0]
– Sam
Sam, I got it, man, but now when none is checked it doesn’t happen anything, all right, but when only one is checked it changes the icon of all
– Israel Gomes
I got brother, I put to check if the input was checked at each loop of the goes and it worked, Thanks for the help!
– Israel Gomes