Jquery toggle checkbox function
Trying to work out why this function runs on load and not on checkbox change as i asked it to.
I'm new to functions and parameters.....
by Matt Greenfield
HTML
<fieldset>
<legend>Categories</legend>
<label>
<input class="checkbox--category" type="checkbox" value="gloves"/>
Gloves
</label>
<label>
<input class="checkbox--category" type="checkbox" value="hats"/>
Hats
</label>
</fieldset>
<fieldset>
<legend>Brands</legend>
<label>
<input class="checkbox--brand" type="checkbox" value="nike"/>
Nike
</label>
<label>
<input class="checkbox--brand" type="checkbox" value="addidas"/>
Addidas
</label>
</fieldset>
<ul class="">
<li class="product gloves nike">Nike Gloves</li>
<li class="product gloves addidas">Addidas Gloves</li>
<li class="product hats addidas">Addidas Hat</li>
<li class="product hats nike">Nike Hat</li>
</ul>
CSS
.product {
display: none;
}
/* This is the class added by the jquery */
.brand--visable,
.category--visable {
display: block;
}
JavaScript
function toggleFilters(type) {
var filter = this.value;
$( '.'+filter ).toggleClass( type+"--visable" );
};
$( document ).ready (function() {
// Run the toggle class functions on checkbox change using a parameter
$( ".checkbox--category" ).change(function(){
toggleFilters("category");
});
$( ".checkbox--brand" ).change(function(){
toggleFilters("brand");
});
});