Reputation: 55263
I have the following:
<div class="pricing-levels-2">
<p>Which level would you like? (Click all that apply)</p>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 1<br>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 2<br>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 3<br>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 4<br>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 5<br>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 6<br>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 7<br>
</div>
<div class="pricing-levels-3">
<p>Which level would you like? (Click all that apply)</p>
<input class="single-checkbox"type="checkbox" name="vehicle" value="Bike">Level 1<br>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 2<br>
<input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 3<br>
</div>
I want the .pricing-levels-2
checkboxes to clear each time I click in one of them, so I did:
jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
jQuery('.pricing-levels-2 .single-checkbox').attr('checked', false);
jQuery(this).attr('checked', true);
});
But the problem is that now ALL the checkboxes clear, even the one I'm selecting. So I can't select any (I want the one I'm clicking to be selected).
What I'm doing wrong?
Upvotes: 0
Views: 15023
Reputation: 10378
by attr
jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
jQuery('.pricing-levels-2 .single-checkbox').removeAttr('checked');
jQuery(this).attr('checked', 'checked');
});
reference removeAttr
by prop
jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
jQuery('.pricing-levels-2 .single-checkbox').prop('checked', false);
jQuery(this).prop('checked', true);
});
Upvotes: 1
Reputation: 78525
You need to use .prop instead:
jQuery('.pricing-levels-2 .single-checkbox').on('click', function(){
jQuery('.pricing-levels-2 .single-checkbox').prop('checked', false);
jQuery(this).prop('checked', true);
});
Upvotes: 5