How do I unselect checkboxes when only one is selected?

1

I have nine checkboxes on my page.

<input id="chkCamp1" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp1', this)">
<input id="chkCamp2" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp2', this)">
<input id="chkCamp3" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp3', this)">
<input id="chkCamp4" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp4', this)">
<input id="chkCamp5" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp5', this)">
<input id="chkCamp6" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp6', this)">
<input id="chkCamp7" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp7', this)">
<input id="chkCamp8" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp8', this)">
<input id="chkCamp9" type="checkbox" style="margin-left: -25px" onclick="showMe('optCamp9', this)">

Just like in radiobuttons, I would like to do the following: When a checkbox is selected, the other nine are deselected. What's the best way to do it?

    
asked by anonymous 23.10.2015 / 13:41

3 answers

1

In javascript you should call a function by selecting checkbox . You can add to onclick() that you already own.

  

In this example, I will use the% (c) class of checkbox to select which ones to enter the function.

First you must select all the elements and scroll through them, deselecting all and at the end select only what you have marked. This function does just that:

function SetSel(elem)
{
  var elems = document.getElementsByClassName("cb");
  var currentState = elem.checked;
  var elemsLength = elems.length;

  for(i=0; i<elemsLength; i++)
  {
    if(elems[i].type === "checkbox")
    {
       elems[i].checked = false;   
    }
  }

  elem.checked = currentState;
}

With your code it would look like this:

<input id="chkCamp1" class="cb" type="checkbox"  onclick=" SetSel(this);"/>1<br/>
<input id="chkCamp2" class="cb" type="checkbox"  onclick=" SetSel(this);"/>2<br/>
<input id="chkCamp3" class="cb" type="checkbox"  onclick=" SetSel(this);"/>3<br/>
<input id="chkCamp4" class="cb" type="checkbox" onclick="SetSel(this);"/>4<br/>
<input id="chkCamp5" class="cb" type="checkbox"  onclick="SetSel(this);"/>5<br/>
<input id="chkCamp6" class="cb" type="checkbox" onclick="SetSel(this);"/>6<br/>
<input id="chkCamp7" class="cb" type="checkbox"  onclick="SetSel(this);"/>7<br/>
<input id="chkCamp8" class="cb" type="checkbox"  onclick="SetSel(this);"/>8<br/>
<input id="chkCamp9" class="cb" type="checkbox"  onclick="SetSel(this);"/>9<br/>

<script>
    function SetSel(elem)
{
  var elems = document.getElementsByClassName("cb");
  var currentState = elem.checked;
  var elemsLength = elems.length;
  
  for(i=0; i<elemsLength; i++)
  {
    if(elems[i].type === "checkbox")
    {
       elems[i].checked = false;   
    }
  }
  
  elem.checked = currentState;
}
</script>

For other examples, this answer might be useful.

    
23.10.2015 / 14:09
0

There is no better way, what exists is solutions that address the problem, in this case I will introduce one of them:

HTML:

<input id="chkCamp1" type="checkbox" data-check="1" value="1"> elemento 1
<input id="chkCamp2" type="checkbox" data-check="2" value="1"> elemento 2
<input id="chkCamp3" type="checkbox" data-check="3" value="1"> elemento 3
<input id="chkCamp4" type="checkbox" data-check="4" value="1"> elemento 4
<input id="chkCamp5" type="checkbox" data-check="5" value="1"> elemento 5<br>
<input id="chkCamp6" type="checkbox" data-check="6" value="1"> elemento 6
<input id="chkCamp7" type="checkbox" data-check="7" value="1"> elemento 7
<input id="chkCamp8" type="checkbox" data-check="8" value="1"> elemento 8
<input id="chkCamp9" type="checkbox" data-check="9" value="1"> elemento 9

JavaScript:

$(function() {
    $('[data-check]').on('click', function() {
        var myInput = {valor: $(this).value, id: $(this).data('check')};
        $('[data-check]').each(function(index, value) {
            ($(this).data('check') == myInput.id) ?
             $(this).attr('checked', true) :
             $(this).attr('checked', false);
        });
    });
});

Here it's working: link

    
23.10.2015 / 14:14
0

You can solve it like this:

$('input').click(function() {
  $('input').prop('checked', false);
  $(this).prop('checked', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
    
23.10.2015 / 14:14