Efficiently refine your search using the combination of checkboxes and dropdown menus simultaneously

I am currently in the process of creating a highly sortable and filterable image gallery that utilizes numerous tags. The inspiration for this project stems from a similar question on Stack Overflow regarding dropdown menus and checkboxes. You can view the original question here where I was able to resolve most of my issues.

Update: I have taken the code from the aforementioned question and expanded upon it further. You can check out the enhanced version here if you're interested in exploring it.

However, I believe there is still room for improvement and wanted to turn it into a little Debugging Game. I have encountered some challenges with the dropdown menus not working ideally in conjunction with the checkboxes.

Here's the link to my project on CodePen: https://codepen.io/manujarvinen/pen/QWvEogb

And here's an image showcasing the project: https://i.sstatic.net/h7Lyv.png

var $filterCheckboxes = $('input[type="checkbox"]');
       var $filtermenues = $('.grid1');

       filterfuncAnother = function () {
           var selectedFilters = [];

           $filtermenues.find(":selected").each(function () {
               debugger
               var v = this.value;
               if (selectedFilters.indexOf(v) === -1 && v)
                   selectedFilters.push(v);
           });

           $('.animal' && '.filterDiv')
               .hide()
               .filter(
                   function (_, a) {
                       var itemCat = $(a).data('category').split(' ');
                       if (itemCat.indexOf("showAll") > -1)
                           return;
                       return selectedFilters.every(
                           function (c) {
                               return itemCat.indexOf(c) > -1;
                           })
                   })
               .show();
       }

       var filterFunc = function () {

           var selectedFilters = [];
           debugger
           $filterCheckboxes.filter(':checked').each(function () {
               var v = this.value;
               if (selectedFilters.indexOf(v) === -1)
                   selectedFilters.push(v);
           });

           $('.animal' && '.filterDiv')
               .hide()
               .filter(
                   function (_, a) {
                       var itemCat = $(a).data('category').split(' ');
                       return selectedFilters.every(
                           function (c) {
                               return itemCat.indexOf(c) > -1;
                           })
                   })
               .show();

       }

       $filterCheckboxes.on('change', filterFunc);

       $('select').on('change', filterfuncAnother);
body {
width: 100%;
text-align: center;
background-color:...
<!-- Based upon this URL: https://stackoverflow.com/q/68315184/4383420 -->

<!-- Help needed in this URL: https://stackoverflow.com/q/68317206/4383420 -->

...

Answer №1

To ensure both elements behave consistently, it's advisable to manage their behavior within the same function:

var $filterCheckboxes = $('input[type="checkbox"]');
       var $filtermenues = $('.grid1');


       var filterFunc = function () {

           var selectedFilters = [];
           $filtermenues.find(":selected").each(function () {
               
               var v = this.value;
               if (selectedFilters.indexOf(v) === -1 && v)
                   selectedFilters.push(v);
           });

           $('.animal' && '.filterDiv')
               .hide()
               .filter(
                   function (_, a) {
                       var itemCat = $(a).data('category').split(' ');
                       if (itemCat.indexOf("showAll") > -1)
                           return;
                       return selectedFilters.every(
                           function (c) {
                               return itemCat.indexOf(c) > -1;
                           })
                   })
               .show();
           $filterCheckboxes.filter(':checked').each(function () {
               var v = this.value;
               if (selectedFilters.indexOf(v) === -1)
                   selectedFilters.push(v);
           });

           $('.animal' && '.filterDiv')
               .hide()
               .filter(
                   function (_, a) {
                       var itemCat = $(a).data('category').split(' ');
                       return selectedFilters.every(
                           function (c) {
                               return itemCat.indexOf(c) > -1;
                           })
                   })
               .show();

       }

       $filterCheckboxes.on('change', filterFunc);

       $('select').on('change', filterFunc);
body {
width: 100%;
text-align: center;
background-color: black;
color: white;
font-family: sans-serif;
}
.grid {
width: 300px;
margin: 50px auto;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
.grid1 {
width: 300px;
margin: 50px auto;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
.filterDiv {
width: 100px;
height: 100px;
padding-top: 20px;
color: black;
font-weight: bold;
}
<!-- Based upon this URL: https://stackoverflow.com/q/68315184/4383420 -->

<!-- Help needed in this URL: https://stackoverflow.com/q/68317206/4383420 -->

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  DEBUGGING GAME!

   <div class=grid1>
          <select>
              <option value="">--</option>
              <option value="violet">violet</option>
          </select>
          <select>
              <option value="">--</option>
              <option value="blue">blue</option>
          </select>
          <select>
              <option value="">--</option>
              <option value="yellow">yellow</option>
          </select>
      </div>

  <div class=grid>
      <label>VIOLET
      <input type="checkbox" value="violet" />
      <span class="checkmark"></span>
      </label>
      <label>BLUE
      <input type="checkbox" value="blue" />
      <span class="checkmark"></span>
      </label>
      <label>YELLOW
      <input type="checkbox" value="yellow" />
      <span class="checkmark"></span>
      </label>
  </div>

  <div class=grid>
      <div class="filterDiv" data-category="violet blue" style="background-color: blue">Tags: <br />violet <br />blue</div>
      <div class="filterDiv" data-category="violet red MVP" style="background-color: red">Tags: <br />violet <br />red <br />MVP</div>
      <div class="filterDiv" data-category="yellow" style="background-color: yellow">Tags: <br />yellow</div>
  </div>


  <div>
  <label>Most Valuable Player (MVP)
  <input type="checkbox" value="MVP" />
  <br />(ANY others than MVP shouldn't be seen at ALL times.<br />BUT it's okay for MVP to be hidden also, if rules aren't met.)
  <span class="checkmark"></span>
  </label>
  </div>

    <div style="width:400px; text-align: left; margin: 60px auto;">
    RULES:
    <p style="color:gray;">*Refresh*</p>
    <p>1. Turn ON MVP. Select BLUE from dropdown > ALL should be hidden.</p>
    <p style="color:gray;">*Refresh*</p>
    <p>2. Select YELLOW from dropdown. Turn on MVP > ALL should be hidden.</p>
    <p style="color:gray;">*Refresh*</p>
    <p>3. Turn ON MVP. Select YELLOW from dropdown > ALL should be hidden.</p>
    <p style="color:gray;">*Refresh*</p>
    <p>BONUS1: Turn ON MVP. Select YELLOW from dropdown. Turn OFF MVP > ONLY YELLOW should be seen.</p>
    <p style="color:gray;">*Refresh*</p>
    <p>BONUS2: Select YELLOW from dropdown. Check YELLOW CHECKBOX. Uncheck YELLOW CHECKBOX > ONLY YELLOW should be seen.</p>
      <p>CONGRATULATIONS!</p>
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am experiencing difficulties in accessing the DOM

I have implemented jQuery $.ajax to dynamically load data into table rows as shown below: <table id='row-data'> <tr><td>1001</td></tr> <tr><td>1322</td></tr> <tr><td>15 ...

Creating a personalized LinkedIn share button through POST requests: here's how

I am interested in using my own image for the LinkedIn share button. After referencing a previous post on this topic: How to make a custom LinkedIn share button It was mentioned that: In order to create a custom Share button for LinkedIn, POST calls need ...

The connection between a database and jQuery AJAX using PHP is not established

Check out this snippet of HTML code: <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="submit" id=& ...

Tips for removing empty space caused by the iPhone notch on your webpage

Hey there, I'm struggling to remove the blank space on my iPhone with a notch at the top of the screen. Do you have any advice on how to change the background color from the default white? My website is live and you can check it out at . I've att ...

Ways to align the content in the middle of a div with CSS

My website is functioning perfectly on large devices, but I am facing an issue when trying to center the icon below the slider on mobile devices. I have used the following code in the icn class: .icn{ margin:0 auto; } However, the icon is not centered as ...

The Jquery onclick function is executing multiple times, with each iteration increasing in

I have encountered an interesting problem that I can't seem to resolve. The issue involves dataTables and the data that is retrieved via jQuery ajax post after a selection change on a select element. Furthermore, I have an onclick function for multipl ...

It seems that using the SVG <mask> tag is necessary for Firefox to display correctly, but it causes issues with CSS mask in

I need assistance with masking content using an external SVG image. Currently, I'm using the following code: #homepage-banner .desktop-slider.masked { -webkit-mask:url(news-panel-mask.svg) left top no-repeat; /* Chrome/Safari (Webkit) */ mask: ur ...

Using -webkit-transform with varying transition durations for rotateX and rotateY properties

Is it possible to have different transition times for both rotateX and rotateY in CSS3? I know you can use -webkit-transition to set a time, but it seems like setting separate transition times for both is not doable. It appears that you can only set it for ...

Ways to showcase a textbox input in different DIVs multiple times

I am struggling to showcase the content of a textarea within multiple div elements when a user inputs text. The value only appears in the first div, but I want it to display in every div. Here is my HTML and jQuery code: <input type="textarea" id="my ...

problem updating data with collapsible panels

I am encountering an issue with my accordion control (using jQuery) on the page. It seems that when I insert the contents of my page into an updatepanel, the accordion stops working. However, if I remove the updatepanel, it starts working again. I'm ...

Exploring the benefits of utilizing useState and localStorage in Next.js with server-side

Encountering an error consistently in the code snippet below: "localstorage is not defined" It seems like this issue arises because next.js attempts to render the page on the server. I made an attempt to place the const [advancedMode, setAdvanced ...

Combining Arrays in AngularJS with an owl-carousel Setting

My goal is to implement an endless scrolling carousel in AngularJS using owl-carousel. The idea is to load new items every time the carousel is fully scrolled and seamlessly merge queried elements with the existing list. However, I've encountered a pr ...

Code that achieves the same functionality but does not rely on the

I utilized a tutorial to obtain the ajax code below. The tutorial referenced the library jquery.form.js. Here is the code snippet provided: function onsuccess(response,status){ $("#onsuccessmsg").html(response); alert(response); } $("# ...

Backend data not displaying on HTML page

I am currently working on an Angular 8 application where I have a service dedicated to fetching courses from an API endpoint. The service method that I'm using looks like this: loadCourseById(courseId: number) { return this.http.get<Cours ...

The output is visible in the Firefox console but does not display on the actual browser

Can anyone help me with a bug in my javascript/jQuery/PHP/mySQL application? When I submit a form, the response is visible in the Firebug console window but not in the browser window. I know this is a vague question, but I'm hoping it might be a commo ...

Error message: "The contract is not defined in thirdweb-dev/react

I am currently using thirdweb-dev/react library to interact with a smart contract. However, I am encountering an issue where the contract is showing up as undefined in my code along with the following error message: query.ts:444 Error: Could not ...

Using the length of an array as an iterator within a nested ngFor loop in Angular 9

I am looping through an array of objects where each object contains another array of objects with attributes like "name" and "id". The length of this array of objects (noticias) varies. I am struggling to display these values and have only managed to acce ...

How can I achieve a similar layout in my .cshtml file?

https://i.sstatic.net/f1C0G.png Hey there, I'm looking to achieve a layout similar to the one shown in the image. Specifically, I want the left side panel to remain unchanged when expanding the Accordion control on the right side. Can someone guide ...

Executing a JavaScript function once an iframe has finished loading

I currently have this code on my index.html file: <body> <iframe id=iframetest src="alt.html"> <p>Your browser does not support iframes.</p> </iframe> <script> $(window).load(function(){ ...

Error message: Unable to locate module without the '.js' extension at the end of the import file path

It seems like the solution is not difficult, just something obvious. I am working on a simple TypeScript project. There are no modules involved, only TypeScript for compilation. The TS files compile into JS files in the dist folder, which are then connect ...