Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to make two filters work at the same time in jquery

I should make my two filtering systems, the one for buttons and the one for text input, work together. Anyone have an idea how to do it?

   $(function() {
        $('input[name="test"]').on('change', function(a, b) {
        value1 = this.value;
        console.log(value1)
        $('#elenco .cliente').hide();
        if (value1 == 'All') {
            $('#elenco .cliente').show();
            dom = $('#elenco .cliente')
        }
        if (value1 == 'PROD') {
            $('#elenco .cliente[value="PROD"]').show();
            dom = $('#elenco .cliente[value="PROD"]')
        }
        if (value1 == 'TEST') {
            $('#elenco .cliente[value="TEST"]').show();
            dom = $('#elenco .cliente[value="TEST"]')
           
        }
        });
    });

    $(document).ready(function(){
        $("#sito").on("keyup", function() {
            var value = $(this).val().toLowerCase();
            //console.log(this)
            $("#elenco .cliente .desc").filter(function() {
            $(this).parent().toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

try something like declaring dom variable at the top of the script.Use it to store a reference to the elements being displayed by the first filtering function. The second filtering function then uses the dom variable to apply the text filter only to the elements. Something like this should work

$(function() {
  var dom; // Declare a variable to store the elements being displayed
  $('input[name="test"]').on('change', function(a, b) {
    value1 = this.value;
    console.log(value1)
    $('#elenco .cliente').hide();
    if (value1 == 'All') {
      $('#elenco .cliente').show();
      dom = $('#elenco .cliente'); // Store the elements being displayed
    }
    if (value1 == 'PROD') {
      $('#elenco .cliente[value="PROD"]').show();
      dom = $('#elenco .cliente[value="PROD"]'); // Store the elements being displayed
    }
    if (value1 == 'TEST') {
      $('#elenco .cliente[value="TEST"]').show();
      dom = $('#elenco .cliente[value="TEST"]'); // Store the elements being displayed
    }
  });

  $(document).ready(function() {
    $("#sito").on("keyup", function() {
      var value = $(this).val().toLowerCase();
      // Apply the text filter only to the elements being displayed
      dom.find('.desc').filter(function() {
        $(this).parent().toggle($(this).text().toLowerCase().indexOf(value) > -1);
      });
    });
  });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading