Coder Guy
Coder Guy

Reputation: 97

Jquery function to toggle all radio buttons on the page as

The question sums it up ... Here's what I've tried, is there a better way? It works once (selects and deselects once) then stops working ...

<a href="#resources" onclick="$(':radio').each(function(){$(this).attr('checked', true); });">(Select All)</a>
<a href="#resources" onclick="$(':radio').each(function(){$(this).attr('checked', false); });">(Deselect All)</a>

Upvotes: 0

Views: 371

Answers (2)

Shukhrat Raimov
Shukhrat Raimov

Reputation: 2257

You can do it like this:

$("#selectAll").click(function() { $('input:radio').prop('checked', true);  });

$("#deselectAll").click(function() { $('input:radio').prop('checked', false); });

No need to use .each function, since jQuery uses implicit iteration anyway. Here is working jsfiddle

Upvotes: 2

beautifulcoder
beautifulcoder

Reputation: 11340

For stuff like this, I recommend:

$('input:radio').each(function() { $(this).prop('checked', false); });
$('input:radio').each(function() { $(this).prop('checked', true); });

Upvotes: 2

Related Questions