AJFMEDIA
AJFMEDIA

Reputation: 2123

jQuery button to check all check boxes issue

I have a button that checks all checkboxes in a div and un-checks.

However if I were to manually check one checkbox, then hit the Check all button and then uncheck all, the checkbox which was manually checked does not become unchecked!

Any ideas?

http://jsfiddle.net/hM5bu/1/

Upvotes: 5

Views: 1723

Answers (2)

Regis
Regis

Reputation: 142

here is a solution:

<input type="checkbox" name="todos" id="todos" /> All<br/>
<input class="marcartodos" type="checkbox" name="marcado[]" value="1" />1<br/>
<input class="marcartodos" type="checkbox" name="marcado[]" value="2" />2<br/>
<input class="marcartodos" type="checkbox" name="marcado[]" value="3" />3<br/>
<input class="marcartodos" type="checkbox" name="marcado[]" value="4" />4<br/>

<script type="text/javascript">

$(function() {

$("#todos").click(function() {                          
if ($(this).is(':checked'))                         
$(".marcartodos").attr('checked', true);
else 
$(".marcartodos").attr('checked', false);
});

});

</script>

Upvotes: -1

Naftali
Naftali

Reputation: 146350

Thats because jQuery was changed in 1.6

Using attr instead of prop is what is breaking it.

Try using prop instead

Updated fiddle: http://jsfiddle.net/hM5bu/2/

See this question: .prop() vs .attr() for more about prop and attr in jQuery 1.6

Upvotes: 6

Related Questions