Reputation: 55263
I have something like this:
<form action="">
<input type="checkbox" name="vehicle" value="Bike">Lesson Audio / <span class="pricing-box-price">$19.95 USD</span><br>
<input type="checkbox" name="vehicle" value="Bike">Lesson PDFs / <span class="pricing-box-price">$19.95 USD</span><br>
<input type="checkbox" name="vehicle" value="Bike">Review Audio / <span class="pricing-box-price">$19.95 USD</span><br>
<div class="pricing-levels">
<p>Which level would you like? (Click all that apply)</p>
<input type="checkbox" name="vehicle" value="Bike">Level 1<br>
<input type="checkbox" name="vehicle" value="Bike">Level 2<br>
<input type="checkbox" name="vehicle" value="Bike">Level 3<br>
<input type="checkbox" name="vehicle" value="Bike">Level 4<br>
<input type="checkbox" name="vehicle" value="Bike">Level 5<br>
<input type="checkbox" name="vehicle" value="Bike">Level 6<br>
<input type="checkbox" name="vehicle" value="Bike">Level 7<br>
</div>
</form>
I want to perform an action when ALL the first three check boxes are unchecked (those above the .pricing-level
div. How to accomplish that? (Because I want to hide the .pricing-level
div when they are all unchecked.)
Upvotes: 0
Views: 88
Reputation: 20830
You can use following :
if($('form > :checked').length == 0){
var x = $('form > :checked').length;
console.log(x);
jQuery('.pricing-levels').hide();
}
jQuery('form > input[name=vehicle]').change(function(){
if($('form > :checked').length == 0){
var x = $('form > :checked').length;
console.log(x);
jQuery('.pricing-levels').hide();
}else{
jQuery('.pricing-levels').show();
}
});
Here is the demo : http://jsfiddle.net/ssfQf/
Upvotes: 0
Reputation: 123739
Try:
if($('form > :checked').length == 0){
//none are checked
}
or more precisely you can do:
if(!$('form > input:checked').length){ //since you don't want to target by name
Upvotes: 3
Reputation: 16961
if($('form > :checked').length === 0){
// Your action
}
This only checks for direct children of the form
element, thus, skipping all those inside .pricing-levels
Upvotes: 1