jQuery clear checkboxes when hiding div?
Question
I have a jQuery script that will display specific divs depending on which select option has been selected.
Here's the code for that:
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery.viewMap = {
'' : jQuery([]),
'1' : jQuery('.company_1'),
'2' : jQuery('.company_2')
};
jQuery('#companyid').change(function() {
jQuery.each(jQuery.viewMap, function() { this.hide(); });
jQuery.viewMap[jQuery(this).val()].show();
});
});
(example div)
<div class="company_1" style="display: none;">
<input type="checkbox" name="classifications[Miner]" id="classifications[Miner]" /> Spec/Rough
<input type="checkbox" name="classifications[Dealer]" id="classifications[Dealer]" /> Dealer
</div>
I'd like it to clear all the checkboxes inside the div (.company_1, .company_2 etc) if someone selects a different option. If that makes sense? :)
Thank you!
Solution
Unchecking a checkbox is achieved by removing the attribute named "checked". Try:
jQuery.each(jQuery.viewMap, function() {
this.hide();
jQuery('input:checkbox', this).removeAttr('checked');
});
OTHER TIPS
@David Andres thanks for pointing me in the right direction I couldn't get your code to work, but used:
$("div.company_1 input:checked").removeAttr("checked");
which did work.
How about:
$("div.company_1 input[type:checkbox]").removeAttr("checked");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow