# Solved: Select all checkboxes JavaScript



## marques_uk (Jun 22, 2009)

Hello,

I'd like to alter some code and I was wondering if anyone who's pretty good at JavaScript might be able to help.

I have an image button that once clicked selects all checkboxes named checklist, once clicked again it unchecks them. I did have a normal input button to do this, so the value of the button was changed according to the state. This JavaScipt code can be seen below.

function Check(chk)
{
if(document.selections.Check_All.value=="Check All"){
for (i = 0; i < chk.length; i++)
chk_.checked = true ;
document.selections.Check_All.value="UnCheck All";
}else{__
for (i = 0; i < chk.length; i++)
chk.checked = false ;
document.selections.Check_All.value="Check All";
}
}

I'd like to alter the code so I can display my own button when its not selected (button.png) and when the checkboxes are all selected I want to display an image (uncheck_button.png).

This is the image button at the moment, it works but only displays the one image.



I hope this is clear, if not I'll try and re-explain.
If anyone could help find a solution to this that would be brilliant.

Thank you_


----------



## Nick Tompson (Jul 12, 2008)

Hello
This code should do it for you:


```
<script type="text/javascript">
function Check( chk )
{
    var state = 0;
    for ( var i = 0; i < chk.length; ++i )
    {
        switch ( chk[ i ].checked )
        {
            case true:
                chk[ i ].checked = false;
                state = 1;
                break;

            case false:
            default:
                chk[ i ].checked = true;
                state = 0;
                break;
        }
    }

    if ( state )
    {
        document.getElementById( "check_img" ).src = "URL TO IMAGE WHEN NOT CHECKED";
    }
    else
    {
        document.getElementById( "check_img" ).src = "URL TO IMAGE WHEN CHECKED";
    }
}
</script>

<img src="URL TO IMAGE WHEN NOT CHECKED" id="check_img" onclick="Check( document.selections.checklist )" />
```
Forgive me if there are any problems, I have not tested it.


----------



## marques_uk (Jun 22, 2009)

That works perfectly Nick, thank you so much!!!


----------



## Nick Tompson (Jul 12, 2008)

Not a problem, you are welcome


----------



## marques_uk (Jun 22, 2009)

Hi Nick/anyone else,

I was wondering if you might be able to help me with another problem I'm having with checkboxes?

I have two sets of checkboxes each with their own 'Select All' button, but I want to add a main select button that will select all of the checkboxes from both the sets of checkboxes. So using the code supplied by Nick, I have two buttons one that uses onClick="Check(document.selections.checklist)" and onClick="Check(document.selections.secondchecklist)" 

So some how I want a button that will take both arguments of the Check function and select buttons in both sets of the checkboxes, (checklist and second checklist).I already use different names and different id's for all of the checkboxes so these can't be used.

I just wish I could find some code that would just use the name of the form to select any checkboxes inside it, this would make things easy, is this possible?

Any help would be brilliant, thank you.


----------



## marques_uk (Jun 22, 2009)

No need to worry found a solution.

onClick="javascript:function1();function2();"

it was that easy, lol...


----------

