Mika
Mika

Reputation: 335

Filtering by multiple selected classes

I've created a Multiple Checkbox Filter

The idea is to select among categories and filter the elements that have AT LEAST one of those categories

At the moment, it's filtering but showing the elements that have ALL of the categories

This is what I'm using:

function run() {
    $("div").hide();
    choice = "";
    $(":checked").each(function() {
       choice += "." + this.id;
    });
    if (choice) 
        $("div" + choice).show();
};    

This is a fiddle to show you how it works.

How can I show the elements that have at least one of the classes checked then?

Upvotes: 4

Views: 77

Answers (1)

Puwka
Puwka

Reputation: 650

You can show them in forEach loop

function run() {
    $("div").hide();
    const checked = $(":checked")
    if (checked.length === 0) {
        return $("div").show();
    }
    checked.each(function() {
        $("div." + this.id).show();
    });
};  


Upvotes: 1

Related Questions