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 8798505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:06:45+00:00 2026-06-14T00:06:45+00:00

This question is a further development of this question: jQuery simple checkbox to hide

  • 0

This question is a further development of this question:

jQuery simple checkbox to hide and show divs (product filtering) which was answered excellently by ManseUK

ManseUK’s solution enabled product filtering by a single category, specifically genre but for my current project I need to sort by 2 categories: colour and size. By modifying ManseUK’s solution I nearly have it working but it fails when I have two filters selected – in this example selecting ‘cyan’ and ‘xl’ should only show the xl cyan but instead it shows both cyans instead of just the xl cyan. The code I’m using is as follows:

HTML:

<input type="checkbox" id="cyan" value="cyan" /> Cyan<br />
<input type="checkbox" id="magenta" value="magenta" /> Magenta<br />
<input type="checkbox" id="black" value="black" /> Black<br />
<input type="checkbox" id="xl" value="xl" /> XL<br />

<!--------------------->
<div class="products">
    <div class="productItem" data-price="250" data-category="cyan">
        Cyan Ink
        <div class="productItemPrice"><span>&pound;250</span></div>
    </div>
    <div class="productItem" data-price="150" data-category="magenta">
        Magenta Ink
        <div class="productItemPrice"><span>&pound;150</span></div>
    </div>
    <div class="productItem" data-price="250" data-category="black">
        Black Ink        <div class="productItemPrice"><span>&pound;250</span></div>
    </div>
    <div class="productItem" data-price="241" data-category="cyan" data-size="xl">
        XL Cyan Ink
        <div class="productItemPrice"><span>&pound;241</span></div>
    </div>
</div>
<!--------------------->​

CSS:

.productItem{float:left;padding:5px;margin:5px;border:1px solid #eaeaea}
body{font-family:verdana}

JS:

$('input[type="checkbox"]').click(function() {
    if ($('input[type="checkbox"]:checked').length > 0) {
        $('.products >div').hide();
        $('input[type="checkbox"]:checked').each(function() {
            $('.products >div[data-category=' + this.id + ']').show();
            $('.products >div[data-size=' + this.id + ']').show();
        });
    } else {
        $('.products >div').show();
    }
});​

JSFIDDLE of the above code

I can see that the js is performing a logical OR on which divs to show – how can I make this an AND?

  • 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-14T00:06:47+00:00Added an answer on June 14, 2026 at 12:06 am

    Made some changes to the code ..

    • Cached the variables.

    • You are looping over all the checkboxes and assigning the id’s to
      it. You need to Query the size separately as it’s a different
      entity..

    • I am looping only the checkboxes that is not size .. All colors

    • Then checking is Size is also check for each color…

    FULL CODE

     $('input[type="checkbox"]').click(function() {
        // Cache These Variables
        var $checked = $('input[type="checkbox"]:checked');
        var $productsDiv = $('.products > div');
        var $sizeCheckBox = $('input[type="checkbox"][value="xl"]');
    
        if ($checked.length > 0) {
            $productsDiv.hide();
            // If only XL Checkbox is Checked
            if ($checked.length == 1 && $sizeCheckBox.is(':checked')) {
                  $('.products > div[data-size=' + $sizeCheckBox.attr('id') + ']')
                     .show();
            } 
            // If Others are also Checked
            else {
                // All Checkboxes and Not Size Checkbox
                $checked.not($sizeCheckBox).each(function() {
                    var dataSize = '';
                    if ($sizeCheckBox.is(':checked')) {
                        dataSize = '[data-size=' + $sizeCheckBox.attr('id') + ']';
                    }
                    $('.products >div[data-category=' + this.id + ']' 
                                 + dataSize).show();
                });
            }
        } else {
            $productsDiv.show();
        }
    });​
    

    Check Fiddle

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

Sidebar

Related Questions

Background (question further down) I've been Googling this back and forth reading RFCs and
This question follows on from a previous question, that has raised a further issue.
Following up this question , I have a further problem - I have two
N.B THIS QUESTION HAS BEEN UPDATED, READ FURTHER DOWN Hi, I want to create
since I couldn't find an answer to this question I researched a bit further
This is a further question based on this answer: How can I implement a
Note: This is a railsier (and more succinct) version of this question , which
Updated Question After some further debugging I've updated this question to be more accurate
I am taking a prior question one step further (see this question ), I
I initially asked this question on the libRocket forum, but given that further investigation

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.