user3275701
user3275701

Reputation: 97

Remove child elements with certain attribute value

I'm trying to filter out some children elements, but I'm not managing to iterate throught children properly. Whats wrong in my code:

$("#filter").on("click", function() {

        $("div#results > div").each(function () {
            if(!$(this).attr('subcategory', 0)){
                $(this).remove();
            }
        });
});

Upvotes: 0

Views: 1258

Answers (3)

Jai
Jai

Reputation: 74748

Why go for .each() loop go for attribute-not-equal-selector:

$("#filter").on("click", function() {
    $('div#results > div[subcategory!="0"]').remove();
});

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388446

Use attribute not equals selector

$("div#results > div[subcategory!=0]").remove()

Upvotes: 3

Milind Anantwar
Milind Anantwar

Reputation: 82251

$(this).attr('subcategory', 0) will set the value to attribute. try this:

$("#filter").on("click", function() {
$("div#results > div").each(function () {
if($(this).attr('subcategory')==="0")){
            $(this).remove();
        }
});});

Upvotes: 1

Related Questions