jeudi 6 octobre 2016

How to check only all-categories checkbox or other remaing category checkboxes in jquery

for example if i check all-categories checkbox in given example all remaining checkboxes should be unchecked and if i check one or more remaining category then all-category checkbox should be unchecked .

here is jsfiddle :http://ift.tt/2duMAwN

HTML

 <div class="search-dropdown">
<input type="checkbox" class="checkbox all-categories" name="all-categories" id="all-categories" value="" /><label for="keys">All categories</label>
<hr>
<ul class="checkboxlist_list">
    <li><input type="checkbox" class="checkbox electical"  /><label>electrical</label><br/></li>
    <li><input type="checkbox" class="checkbox mechanical" /><label>mechanical</label><br/></li>
    <li><input type="checkbox" class="checkbox construction"/><label>construction</label><br/></li>
</ul>
</div>
<p class="category-holder">Select Category</p>

Javascript

    $('.search-dropdown input[type="checkbox"]').on("change", function(){
    var categories = [];
    $('.checkbox:checked').each(function(){        
        var category = $(this).next().text();
        categories.push(category);
    });
    $(".category-holder").html(categories.join(", "));
    if (!$(".category-holder").text().trim().length) {
    $(".category-holder").text("Select Category");
    }
});

Thanks




Aucun commentaire:

Enregistrer un commentaire