0

I am using the following jquery to achieve check all effect in a php page:-

<script>
$(function(){
    $("#checkall").click(function(){
        if($("#checkall").attr('checked')){
            $(".department").attr('checked', true);
        }else{
            $(".department").attr('checked', false);
        }
    });
});
</script>

<input type="checkbox" id="checkall" name="check_all" />

<input type="checkbox" name="department[]" class="department" value=""  checked = "checked"/>

and the check all and uncheck all effect works fine. However, it doesn't work with the reset button in the form. How shall I modify the jquery? Thanks!

0

3 Answers 3

1
$("#checkall").click(function(){
    $(".department").prop('checked', $(this).is(':checked') ); 
});

The jQuery .prop() docs has a section describing why this should be used instead of manipulating the attribute.

Sign up to request clarification or add additional context in comments.

Comments

1

Use .prop instead .attr, see the working demo.

$(function(){
    $("#checkall").click(function(){
        if($(this).prop('checked')){
            $(".department").prop('checked', true);
        }else{
            $(".department").prop('checked', false);
        }
    });
});​

And your code could be even short:

$(function(){
    $("#checkall").click(function(){
        $(".department").prop('checked', $(this).prop('checked'));
    });
});​

Comments

1

Try this demo : http://jsfiddle.net/JCWaT/

My old reply in here: checkbox check all option

Hope fits the need :)

or for your need http://jsfiddle.net/kJCcP/

code

$('input[name="Company"],input[name="Country"]').click(function(){

          $(this).nextAll('input[name="'+this.name+'Sub"]').prop('checked',this.checked);

});
​

OR

$('#checkall').click(function() {

    $(this).nextAll('.department').prop('checked', this.checked);

});​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.