Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7029499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:32:49+00:00 2026-05-28T00:32:49+00:00

I want to filter some content using checkboxes. I’ve managed to do that thanks

  • 0

I want to filter some content using checkboxes.

I’ve managed to do that thanks to an earlier post which I’ve simplified a bit here DEMO.

My problem is that each content item can have more than one category attached.When I select category A and category B and then deselect category B, the content item that have both categories attached are removed.

The project I’m working on is going to contain more than two categories. A content item can have many categories attached

HTML:

<ul id="filters">
    <li>
        <input type="checkbox" value="categorya" id="filter-categorya" />
        <label for="filter-categorya">Category A</label>
    </li>
    <li>
        <input type="checkbox" value="categoryb" id="filter-categoryb" />
        <label for="filter-categoryb">Category B</label>
    </li>
</ul>

<div class="categorya categoryb">A, B</div>
<div class="categorya">A</div>
<div class="categorya">A</div>
<div class="categorya">A</div>
<div class="categoryb">B</div>
<div class="categoryb">B</div>
<div class="categoryb">B</div>

Javascript:

$('input').click(function() {
    var category = $(this).val();

    if (!$(this).attr('checked')) $('.' + category).hide();
    else $('.' + category).show();

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:32:49+00:00Added an answer on May 28, 2026 at 12:32 am

    I think the two most straightforward approaches would be on click of any of the filter checkboxes either:

    1. Hide all <div> elements, then loop through the checkboxes and for each checked one .show() the <div> elements with the associated category.

    2. Loop through all checkboxes to make a list of the classes to be shown, then loop through the <div> elements, checking each one to see if it has one of those classes and .hide() or .show() as appropriate.

    Is there some general selector you can use to get all the <div> elements? Do they have a particular container element, like how all the checkboxes are descendents of “#filters”?

    // Solution 1
    
    $("#filters :checkbox").click(function() {
       $("div").hide();
       $("#filters :checkbox:checked").each(function() {
           $("." + $(this).val()).show();
       });
    });
    

    Demo: http://jsfiddle.net/6wYzw/41/

    // Solution 2
    
    $("#filters :checkbox").click(function() {
    
       var re = new RegExp($("#filters :checkbox:checked").map(function() {
                              return this.value;
                           }).get().join("|") );
       $("div").each(function() {
          var $this = $(this);
          $this[re.source!="" && re.test($this.attr("class")) ? "show" : "hide"]();
       });
    });
    

    Demo: http://jsfiddle.net/6wYzw/42/

    I guess the first way is shorter, and easier to maintain…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to filter some reserved word on my title form. $adtitle = sanitize($_POST['title']);
We have a situation where we want to use filter for URL's containing some
I have a div which has 14 child divs with some content. Now what
I've a content type which content some cck field. One of the field has
I just want to remove the br tags. '<div class=temp>some content<br>and more<br><a href=#> and
I'm trying to filter content in Nanoc 3 using their :redcarpet filter. My code
I want to filter an array, using the array_filter function. It hints at using
I want to filter the selectable dates on a datepicker. I basically need to
I want to filter a java.util.Collection based on a predicate.
I want to filter repeated elements in my list for instance foo = ['a','b','c','a','b','d','a','d']

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.