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

  • Home
  • SEARCH
  • 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 9075853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:57:59+00:00 2026-06-16T18:57:59+00:00

I have many articles which have multiple categories. I have a checkbox for every

  • 0

I have many articles which have multiple categories. I have a checkbox for every category. When a checkbox is checked the articles with this category should be shown, otherwise should be hidden.

So far so good.

When I check category 1, but not category 2 all posts should be shown which have category 1. If they have category 2 too they should be shown anyway.

<input type="checkbox" value="cat1" id="checkbox1" checked />
<input type="checkbox" value="cat2" id="checkbox2" checked />
<input type="checkbox" value="cat3" id="checkbox3" checked />
<input type="checkbox" value="cat4" id="checkbox4" checked />

I use jQuery to show and hide the posts

if ($('#checkbox1').is(':checked')) {
    $("article.category1").show();
} else {
    $("article.category1").hide();
}

if ($('#checkbox2').is(':checked')) {
    $("article.category2").show();
} else {
    $("article.category2").hide();
}

if ($('#checkbox3').is(':checked')) {
    $("article.category3").show();
} else {
    $("article.category3").hide();
}

if ($('#checkbox4').is(':checked')) {
    $("article.category4").show();
} else {
    $("article.category4").hide();
}

I created a js fiddle for this
http://jsfiddle.net/oliverspies/t8qHT/1/

If you uncheck all, but check the first checkbox, no entries are shown – but all entries with the class cat1 should be shown – even if they have another category which is unchecked.
How can I do this without writing tens of IF statements?

  • 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-06-16T18:58:01+00:00Added an answer on June 16, 2026 at 6:58 pm

    Fixed code

    You can create a selector based on the input’s value.

    $('#catselector').on("change", function() {
      $("article").hide();
      $("input[type=checkbox]:checked", this).each(function(){
        $("article." + this.value).show();
      });
    });
    

    See it here.


    Previous code

    $('#catselector').on("change", function() {
      $("input[type=checkbox]", this).each(function(){
        $("." + this.value).toggle(this.checked);
      });
    });
    

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

Sidebar

Related Questions

I have come across many articles which warn against using links to provide logout
I have seen many articles about POCO. What is this?
I have a Model called Section which has many articles ( Article ). These
I have seen in many articles on Sql Server which states that when we
I have read many articles about linq to sql performance. The result which i
I have a model Articles that has_many Assets which is a polymorphic model that
I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
I am trying understand ViewModels deeper and I have read many articles and blogs
I want to translate text in Indian languages. I have gone through many articles
I have two model: User, Article A user can like or dislike many articles,

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.