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() {
- if ( $('input:checkbox[name=shape]:checked').length > 0)
- {
- $(".box").each(function(){
- $(this).removeClass('fadeInLeft').addClass('fadeOutLeft').css('display','none').css('display','none');
- });
- let data = [];
- $('input:checkbox[name=shape]:checked').each(function() {
- data.push($(this).data('shape'));
- });
- console.log(data);
- $.each(data, function(index, value){
- $('.box[data-shape="'+value+'"]').removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
- });
- }
- else
- {
- $(".box").each(function(){
- $(this).removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
- });
- }
- });
- $('input:checkbox[name=color]').on('change', function() {
- if ( $('input:checkbox[name=color]:checked').length > 0)
- {
- $(".box").each(function(){
- $(this).removeClass('fadeInLeft').addClass('fadeOutLeft').css('display','none').css('display','none');
- });
- let data = [];
- $('input:checkbox[name=color]:checked').each(function() {
- data.push($(this).data('color'));
- });
- $.each(data, function(index, value){
- $('.box[data-color="'+value+'"]').removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
- });
- }
- else
- {
- $(".box").each(function(){
- $(this).removeClass('fadeOutLeft').addClass('fadeInLeft').css('display','block');
- });
- }
- });
Thanks