Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery. How to uncheck all checkboxes except one (which was checked)

I have html items, 4 example:

<div>
    <div><input type="checkbox" class="foo"> Checkbox</div>
    <div><input type="checkbox" class="foo"> Checkbox</div>
    <div><input type="checkbox" class="foo"> Checkbox</div>
    <div><input type="checkbox" class="foo"> Checkbox</div>
    <div><input type="checkbox" class="foo"> Checkbox</div>
    <div><input type="checkbox" class="foo"> Checkbox</div>
</div>

And now, I want next: If I click on any checkbox - the checkbox should be checked and other checkboxes should be unchecked. How can I do it?

like image 412
Andrew Avatar asked Dec 06 '12 07:12

Andrew


People also ask

How do you uncheck a selected checkbox when one checkbox is unchecked?

Select change eventCheck total options and total selected options if it is equal then set Select All checkbox checked otherwise unchecked it.

How can I uncheck checkbox is checked in jQuery?

By using jQuery function prop() you can dynamically add this attribute or if present we can change its value i.e. checked=true to make the checkbox checked and checked=false to mark the checkbox unchecked.

How do you uncheck select all checkbox when any one of their child got deselected?

Basically on click of any checkbox, if the "checked" prop is false, uncheck the Select All Checkbox. $("input[type=checkbox]").

How do you uncheck a checkbox when another is checked in react?

To uncheck a checkbox programmatically in React, we can set the checked prop of the checkbox to a state. We have the checked state that we used to set the checked prop of the checkbox. Then we add a button that calls setChecked to toggle the checked value when we click the button.


3 Answers

You can use radio button and group them by name attributes. If you have to do it with checkboxes then you can do it this way,

Live Demo

$('.foo').click(function(){     
      $('.foo').attr('checked', false);
      $(this).attr('checked', true);          
});

For dynamically generated html you need on that allows you to delegate the event with static parent, you can use document with you do not know static parent.

$(document).on('click','.foo', function(){     
      $('.foo').attr('checked', false);
      $(this).attr('checked', true);          
});

Document could be replaced by static parent if it is known. e.g. if div contain dynamically generated has id parentdiv then you can have

`$('#parentdiv').on('click','.foo', function(){`

Edit

Use prop instead of attr for properties checked, selected, or disabled as per documentation.

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.

like image 157
Adil Avatar answered Oct 23 '22 02:10

Adil


Another solution

$('.foo').click(function(){     
      $('.foo').not(this).attr('checked', false);      
});
like image 27
Joyal Avatar answered Oct 23 '22 02:10

Joyal


jQuery V: 1.6 =<

$( ".foo" ).change(function() {
    $('.foo').not(this).prop('checked', false);
});
like image 2
Sadee Avatar answered Oct 23 '22 02:10

Sadee