My input checkbox does not come with JavaScript

1

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>
    
asked by anonymous 05.03.2018 / 20:36

3 answers

2

Another alternative is each time you click on one element it unchecks all the others. For this you can use filter to remove only the element which has, and then applies checked = false to those that are left:

Example:

const boxes = document.querySelectorAll(".t");
boxes.forEach(boxClick => boxClick.addEventListener("click",function(){
  [...boxes].filter(box => box != this).forEach(box => box.checked = false);
}));
#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">
  <input type="checkbox" class="t">
</div>

Note that querySelectorAll returns an NodeList that does not have the filter method. That's why I've used spread operator which are ... in:

[...boxes]

In order to create a normal array and be able to use filter , you have filtered by removing only the box that is being clicked:

filter(box => box != this)

And after removing the box clicked I applied checked = false to the rest with:

forEach(box => box.checked = false)

With this solution you can have as many checkboxes you want it to work as you want, always leaving at most one selected.

    
05.03.2018 / 23:28
1

You can use previousElementSibling and nextElementSibling to know which checkbox is clicking.

When you click on the first checkbox , the previousElementSibling will be null , so it's easy to know if the checkbox clicked was second, then just uncheck the other.

See:

var box = document.querySelectorAll(".t");

box[0].addEventListener("click",selecionarGru);
box[1].addEventListener("click",selecionarGru);

function selecionarGru(){

   var obj1 = this.previousElementSibling,
       obj2 = this.nextElementSibling;

   (obj1 || obj2).checked = false;

}
#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>
    
05.03.2018 / 22:14
0

Try this:

<script> 
var elements  = document.querySelectorAll('.t');
elements.forEach(function(value, key){
    elements[key].addEventListener("click",selecionar);
});

function selecionar(item)
{
    let marked = this.checked;

    elements.forEach(function(value, key){
    elements[key].checked = false;
  });

  if(marked === true) this.checked = true;
}
</script>

And test here: link

    
05.03.2018 / 21:53