Bill
Bill

Reputation: 1477

check all checkboxes jQuery

I have this code:

if $('#myDiv').find('input[type=checkbox]:checked').removeAttr('checked');

Unchecks all checkboxes is there a line of code that will add the check to all of the checkboxes without looping each checkbox?

I've tried:

$('#myDiv' input[type="checkbox"]').prop('checked', $(this).prop('checked'));

But that did not work. Can someone help me?

Upvotes: 0

Views: 82

Answers (2)

Nabil Ali
Nabil Ali

Reputation: 157

Try this:

 $('#myDiv input:checkbox').attr('checked',true);

As per the accepted answer this only works for jquery 1.5.x and below.

Upvotes: -1

Mati
Mati

Reputation: 1148

Try

jQuery 1.6+

$('#myDiv input:checkbox').prop('checked', true);

jQuery 1.5.x and below

$('#myDiv input:checkbox').attr('checked', true);

Note: $( ":checkbox" ) is equivalent to $( "[type=checkbox]" )

Upvotes: 3

Related Questions