Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
How to the show/hide the correct elements with 2 checkboxes in jQuery

I have two checkboxes to filter through recipes (stored on a database) on a website. The allergy filter HIDES all the recipes with the allergy selected but the diet filter SHOWS only the recipes with the diet selected. (i.e. if the user checks the peanut allergy and gluten free it will hide recipes with those filters.) Both checkboxes work individually, but having both of them checked shows the wrong recipes. For example, what I want to happen is if the user checks off peanut allergy and the vegan diet, I want to show recipes that have no peanuts and are vegan. I basically need a way to keep track of what recipes are showing from one checkbox and filter through those for the other checkbox.

Below is the code I have:

    function filterFilesList() {
        var rows = $('.file-row');

        var checkedAllergies = $("#filterControlsAllergies :checkbox:checked");
      var checkedDiet = $("#filterControlsDiet :checkbox:checked");


        if(checkedAllergies.length && checkedDiet.length){
          console.log("both selected");
          rows.show(200);
          var type1 = []
          var arr = checkedAllergies.map(function(){
             type1.push("." + $(this).val())

          }).get();
          var type2 = []
          var arr = checkedDiet.map(function(){
             type2.push("." + $(this).val())

          }).get();

          //Filter through database and show/hide recipes depending on both checkboxes


        } else if(checkedAllergies.length){
            rows.show(200);
          var type1 = []
            var arr = checkedAllergies.map(function(){
                 type1.push("." + $(this).val())

            }).get();

          for (let i = 0; i < type1.length; i++){
            $(type1[i]).hide(200);
          }



        } else if(checkedDiet.length){
          rows.hide(200);
          var type2 = []
          var arr = checkedDiet.map(function(){
             type2.push("." + $(this).val())

          }).get();

          for (let i = 0; i < type2.length; i++){
            $(type2[i]).show(200);
          }



          } else {
            rows.show();
          }
  }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you have two different ways to solve this problem.

  1. Check API, use existing or create new filter by allergies and diet.
  2. Filter at client side. To do this you need:
  • Get list of recipes that should be hidden by allergy.
  • Get recipes by diet. Show diet recipes only if they are absent in allergy list.

Please check code below:

if(checkedAllergies.length && checkedDiet.length){
  rows.hide(200);
  var type1 = []
     checkedAllergies.map(function(){
     type1.push("." + $(this).val())

  }).get();
  
  //let's create set of allergies recipes, that we need to exclude
  let allergiesSet = new Set();
  for(let i=0;i<type1.length;i++){
    allergiesSet.add(type1[i]);
  }
  
  var type2 = []
  var arr = checkedDiet.map(function(){
     type2.push("." + $(this).val());
  }).get();

  //Filter through database and show/hide recipes depending on both checkboxes

  for (let i = 0; i < type2.length; i++){
    if(!allergiesSet.has(type2[i])) {$(type2[i]).show(200);};
  }
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda