DOM Multi Level Filtering based on Different Data Attributes

DOM Multi Level Filtering based on Different Data Attributes

Can you please take a look at This Demo and let me know how I can enrich my code to have multi level filtering system based on different existing and potential upcomming data attributes?

As you can see I am trying to filter the .box based on two data attributes shape and color but my code is filtering the DOM separately. Can you please let me know how I fix this?


  • $('input:checkbox[name=shape]').on('change', function() {
    1.    if ( $('input:checkbox[name=shape]:checked').length > 0)
    2.    {
    3.       $(".box").each(function(){
    4.          $(this).removeClass('fadeInLeft').addClass('fadeOutLeft').css('display','none').css('display','none');
    5.       });
    6.       let data = [];
    7.       $('input:checkbox[name=shape]:checked').each(function() {
    8.          data.push($(this).data('shape'));
    9.       });
    10.       console.log(data);

    11.       $.each(data, function(index, value){
    12.          $('.box[data-shape="'+value+'"]').removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
    13.       });
    14.    }
    15.    else
    16.    {
    17.       $(".box").each(function(){
    18.          $(this).removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
    19.       }); 
    20.    }

    21. });

    22. $('input:checkbox[name=color]').on('change', function() {
    23.    if ( $('input:checkbox[name=color]:checked').length > 0)
    24.    {
    25.       $(".box").each(function(){
    26.          $(this).removeClass('fadeInLeft').addClass('fadeOutLeft').css('display','none').css('display','none');
    27.       });

    28.       let data = [];
    29.       $('input:checkbox[name=color]:checked').each(function() {
    30.          data.push($(this).data('color'));
    31.       });


    32.       $.each(data, function(index, value){
    33.          $('.box[data-color="'+value+'"]').removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
    34.       });
    35.    }
    36.    else
    37.    {
    38.       $(".box").each(function(){
    39.          $(this).removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
    40.       }); 
    41.    }

    42. });


    Thanks