Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check other checkboxes when check one box

I have a checkbox in my page. When I check it, other check boxes should be checked. And when I uncheck it, all the other check boxes should be unchecked too. How can I do this using jQuery? Or is simple HTML is enough?FIDDLE

    <div>Select All<input type="checkbox" checked="true"></div> 
    1.<input type="checkbox"> 
    2.<input type="checkbox"> 
    3.<input type="checkbox"> 
    4.<input type="checkbox"> 
    5.<input type="checkbox"> 
like image 697
Benny Avatar asked Oct 04 '13 06:10

Benny


People also ask

How do I select all checkboxes with one checkbox?

In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.

How do you check if checkbox is already checked?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

How do you check only one checkbox at a time?

If you need to select one checkbox at a time in multiple checkbox, i mean you want to allow anly one checkbox user can check then you can do easily using jquery. jquery prop() through you can give attribute value checked equel to true or false.

How do I make a checkbox automatically checked?

Add checked = "checked" to the input you want selected. For XHTML the recommended way is as described. Check HTML manual and W3C to confirm. The markup posted isn't XHTML, so it's logical to use just the simple keyword attribute checked .


3 Answers

Usig jQuery you can do like this

HTML:

<input id='selectall' type="checkbox" checked="true">

JS:

$('#selectall').change(function () {
    if ($(this).prop('checked')) {
        $('input').prop('checked', true);
    } else {
        $('input').prop('checked', false);
    }
});
$('#selectall').trigger('change');

Check this in fiddle

like image 78
Praveen Avatar answered Oct 07 '22 07:10

Praveen


you can try this:

            <html>
            <head>
            <script type="text/javascript">
            function toggleGroup(id) {
                var group = document.getElementById(id);
                var inputs = group.getElementsByTagName("input");
                for (var i = 0; i < inputs.length; i++) {
                    inputs[i].checked = (inputs[i].checked) ? false : true;
                }

            }
            window.onload = function() {
            document.getElementById("checkmain").onchange = function() {
               toggleGroup("check_grp");

            }
            }
            </script>
            </head>
            <body>
             <form name="form1" >
                <input type="checkbox" name="checkmain" id="checkmain" /><br />
                <p id="check_grp">
                     <input type="checkbox" /><label for="check1">check 1</label>
                     <input type="checkbox"  /><label for="check2">check 2</label>
                     <input type="checkbox"  /><label for="check3">check 3</label>
                     <input type="checkbox"  /><label for="check4">check 4</label>
                     <input type="checkbox"  /><label for="check5">check 5</label>
                </p>
            </form>
            </body>
            </html>
like image 38
shemy Avatar answered Oct 07 '22 05:10

shemy


Try this,

<div>Select All<input type="checkbox" id="parent" /></div> 
  1.<input type="checkbox" class="child"> 
  2.<input type="checkbox" class="child"> 
  3.<input type="checkbox" class="child"> 
  4.<input type="checkbox" class="child"> 
  5.<input type="checkbox" class="child"> 

<script>
    $(function(){
      $('#parent').on('change',function(){
         $('.child').prop('checked',$(this).prop('checked'));
      });
      $('.child').on('change',function(){
         $('#parent').prop('checked',$('.child:checked').length ? true: false);
      });
    });
</script>

Fiddle

You can achieve it like,

$(function() {
  $('#parent').on('change', function() {
    $('.child').prop('checked', this.checked);
  });
  $('.child').on('change', function() {
    $('#parent').prop('checked', $('.child:checked').length===$('.child').length);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>Select All<input type="checkbox" id="parent" /></div>
1.<input type="checkbox" class="child"> 2.
<input type="checkbox" class="child"> 3.
<input type="checkbox" class="child"> 4.
<input type="checkbox" class="child"> 5.
<input type="checkbox" class="child">
like image 44
Rohan Kumar Avatar answered Oct 07 '22 05:10

Rohan Kumar