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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:40:43+00:00 2026-06-11T14:40:43+00:00

I have two options that I am trying to filter between now, but will

  • 0

I have two options that I am trying to filter between now, but will be adding in a third and maybe a fourth later on (for example: right now I am filtering between prices and reviews but will want to add more prices to the list and also another filter category like “rating” which would consist of 3, 4, or 5 stars).

Using the logic that I have listed below works fine, but I feel it will get really long and complicated (and unnecessary) as I go. I know there is a way to refactor the code I’m just wondering what the best way to go about it would be?
HTML:

  <select class="deals">
    <option value="deals-all">All deals</option>
    <option value="50">$50</option>
    <option value="25">$25</option>
  </select>

  <select class="reviews">
    <option value="reviews-all">All reviews</option>
    <option value="reviews-positive">Positive reviews</option>
    <option value="reviews-negative">Negative reviews</option>
  </select>

jQuery

 $('.reviews, .deals').change(function() {

var reviewsVal = $('.reviews :selected').val();
var dealsVal = $('.deals :selected').val();



 if((reviewsVal == 'reviews-all') && (dealsVal == 'deals-all')) {

      $('.review-positive').show();
 $('.review-negative').show();
 $('.deals-25').show();
 $('.deals-50').show();
 } 


else if((dealsVal == '50') && (reviewsVal == 'reviews-positive')) {
 $('.review-negative.deals-50').hide();
 $('.review-positive.deals-50').show();
 $('.review-negative.deals-25').hide();
 $('.review-positive.deals-25').hide();

 }

 else if((dealsVal == '50') && (reviewsVal == 'reviews-negative')) {
 $('.review-negative.deals-50').show();
 $('.review-positive.deals-50').hide();
 $('.review-negative.deals-25').hide();
 $('.review-positive.deals-25').hide();
 }

else if((dealsVal == '25') && (reviewsVal == 'reviews-positive')) {
 $('.review-negative.deals-50').hide();
 $('.review-positive.deals-50').hide();
 $('.review-negative.deals-25').hide();
 $('.review-positive.deals-25').show();
}

else if((dealsVal == '25') && (reviewsVal == 'reviews-negative')) {
 $('.review-negative.deals-50').hide();
 $('.review-positive.deals-50').hide();
 $('.review-negative.deals-25').show();
 $('.review-positive.deals-25').hide();
}

 else if((dealsVal == 'deals-all') && (reviewsVal == 'reviews-positive')) {
 $('.review-negative.deals-50').hide();
 $('.review-positive.deals-50').show();
 $('.review-negative.deals-25').hide();
 $('.review-positive.deals-25').show();
}

else if((dealsVal == 'deals-all') && (reviewsVal == 'reviews-negative')) {
 $('.review-negative.deals-50').show();
 $('.review-positive.deals-50').hide();
 $('.review-negative.deals-25').show();
 $('.review-positive.deals-25').hide();
}

else if((dealsVal == '50') && (reviewsVal == 'reviews-all')) {
 $('.review-negative.deals-50').show();
 $('.review-positive.deals-50').show();
 $('.review-negative.deals-25').hide();
 $('.review-positive.deals-25').hide();
}

else if((dealsVal == '25') && (reviewsVal == 'reviews-all')) {
 $('.review-negative.deals-50').hide();
 $('.review-positive.deals-50').hide();
 $('.review-negative.deals-25').show();
 $('.review-positive.deals-25').show();
}

else {
 $('.review-positive').show();
 $('.review-negative').show();
 $('.deals-25').show();
 $('.deals-50').show();
} 
});

$('.reviews-positive').click(function() {
$('.review-negative').hide();
$('.review-positive').show();
});

$('.reviews-negative').click(function() {
$('.review-positive').hide();
$('.review-negative').show();
});


});​

Hopefully you can see what I am going for, thanks for any input.

*EDIT: jsfiddle: http://jsfiddle.net/TXywp/1/

  • 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-11T14:40:44+00:00Added an answer on June 11, 2026 at 2:40 pm

    To get a dynamic result a standardized naming is the best way to go.
    You start by standardizing the criteria selectors, giving them a general class and differentiating with id properties. Following a naming convetion for values is also recommended. This would produce something in the lines of

        <select id="deal" class="criteriaSelector">
          <option value="all">All deals</option>
          <option value="50">$50</option>
          <option value="25">$25</option>
        </select>
    
        <select id="review" class="criteriaSelector">
          <option value="all">All reviews</option>
          <option value="positive">Positive reviews</option>
          <option value="negative">Negative reviews</option>
        </select>
    

    Then you give all the items you want to filter a generic class besides the filtering classes. I will use .item as an example. You will also want to be able to construct the class from selected value and criteria name so using the .{id}-{value} system will work perfectly. This will leave us with something in the lines of

        <div class="item review-positive deal-25"></div>
        <div class="item review-negative deal-50"></div>
    

    This setup will allow us to build dynamic and extensible code:

        $('.criteriaSelector').change(function() {
          // Initialize criteria string
          var criteria = '';
          // Set value for all selector
          var showAll = true;
    
          // Iterate over all criteriaSelectors
          $('.criteriaSelector').each(function(){
            // Get value
            var val = $(this).children(':selected').val();
            // Check if this limits our results
            if(val !== 'all'){
              // Append selector to criteria
              criteria += '.' + $(this).attr('id') + '-' + val;
              // We don't want to show all results anymore
              showAll = false;
            }
          });
          // Check if results are limited somehow
          if(showAll){
            // No criterias were set so show all
            $('.item').show();
          } else {
            // Hide all items
            $('.item').hide();
            // Show the ones that were selected
            $(criteria).show();
          }
    
        });
    

    Now adding a new criteria selector won’t require changes in this piece of code as long as naming conventions are followed.

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

Sidebar

Related Questions

I have an application that has two localization options at the moment through .resx
how can I copy the selected option between two s that have the same
I am implementing slideshow. I have two options for paging between images: 1) to
I'm trying to connect a qcombobox with a qwtslider, and I have two options
I am trying to figure out relationships and deletion options. I have two tables,
I'm trying to create a regular expression that will pick the longest of two
I want to parse a large XML file and I have two options: Perl
Hy, I have two options to choose from: Client side(with FLASH or Javascript): pro:
I have a ADO.NET/TSQL performance question. We have two options in our application: 1)
I need to write data into drive. I have two options: write raw sectors.(_write(handle,

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.