Check all checkboxes (javascript)

javascript checkbox select all

Here is the javascript code for checking all the checkboxes inside an html document.

<script>
function checkAll()
{

var inputs = document.getElementsByTagName(‘input’);
var checkboxes = [];
for (var i = 0; i < inputs.length; i++) {

  if (inputs[i].type == ‘checkbox’) {
inputs[i].checked =true;

}
}
}

</script>

12 Comments

12 Responses to “Check all checkboxes (javascript)”

  1. amanda July 8, 2008 at 7:56 pm #

    this was extremely helpful, thanks for posting!

  2. Sajan August 12, 2008 at 10:38 am #

    Hi, the code was really helpul. I would like to know something more about this. I am trying to put in code where if I select all the checkboxes the selectall checkbox should automatically get selected.
    For eg: I have five rows in a table with check option. I click select all and the above code works. Now can u help me such that when I click each of the checkboxes for these 5 rows the above select all checkbox gets selected. The reverse of what happened with the query. Plz help me out..

  3. Ranjan January 23, 2009 at 10:42 am #

    Hi Sajan ,

    Here’s modified code.
    The logic is just check whether check box SelectAll is checked or not, if checked then select all else un select all.

    Check for the name before you select it.
    here I have checked for name “chk” and type checkbox.

    function checkAll() {
    var selectAll = document.getElementById(‘SelectAll’);

    var inputs = document.getElementsByTagName(‘input’);
    for (var i = 0; i < inputs.length; i++)
    {
    if (inputs[i].type == ‘checkbox’ && inputs[i].name == “chk”) {
    if(selectAll.checked==true)
    inputs[i].checked = true;
    else
    inputs[i].checked = false;
    }
    }
    }

  4. Ranjini May 14, 2009 at 5:45 pm #

    I have a requirement where I need to pass the fields which have checkboxes to another page. Like if i have checkboxes on item field, i have to pass all those items(which are selected onclick of selectall button) to another page.

  5. Khalid July 6, 2009 at 5:45 pm #

    THIS is the script I’ve been searching for. Many Thanks!

  6. Michael Markie November 18, 2009 at 4:31 pm #

    This code can be cut down slightly by doing the following:

    function checkAll(){
    var t = document.getElementsByTagName(‘input’);
    for(var i in t)
    if(t[i].type==’checkbox’)
    t[i].checked=true;
    }

  7. Daxa July 15, 2010 at 11:21 am #

    Its good.

  8. Mahdi Javaheripur November 10, 2010 at 12:37 pm #

    Hi,

    This can be hugely optimized for forms with a lot of checkboxes if you get inputs.length before the loop so the input.length won’t be calculated in every iteration of the loop.

    var inputsLength = inputs.length;
    for (var i = 0; i < inputsLength; i++)

  9. atul October 10, 2011 at 10:29 am #

    hi how can we select check box when they are dynamically craeted in nested foreach loops……………..

  10. sreeram December 15, 2011 at 1:26 pm #

    Thanks for a such a valuable information……..

Leave a Reply

More in php (83 of 101 articles)


If you want to randomize a php array , you can use shuffle function. But we can't randomize the ...