What I want to do with checkbox
, is the same as radio
does using JavaScript .
Why do not I just use radio
?
It's because if radio
is checked, there's no way I can leave it unchecked.
I know the logic in JavaScript is right, because I can see by console.log
, but when I click on another input
it does not uncheck what is marked.
var obj2 = "";
var box = document.querySelectorAll(".t");
box[0].addEventListener("click",selecionarGru);
box[1].addEventListener("click",selecionarGru);
function selecionarGru(){
if(obj2 == "" || obj2 == this){
if(obj2 == this){
obj2.removeAttribute('checked');
obj2 = "";
return;
}
obj2 = this;
obj2.setAttribute("checked","");
}else{
obj2.removeAttribute('checked');
obj2 = this;
obj2.setAttribute("checked","");
}
}
#tudo1{
background: green;
width: 200px;
height: 200px;
}
.t{
width: 20px;
height: 20px;
margin: 30px 0 0 80px;
background: red;
}
<div id="tudo1">
<input type="checkbox" class="t">
<input type="checkbox" class="t">
</div>