Home > Software design >  how to make two filters work at the same time in jquery
how to make two filters work at the same time in jquery

Time:12-28

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)
            });
        });
    });

CodePudding user response:

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);
      });
    });
  });
});
  • Related