-1

Is it possible to target the last visible div/container after a js function has worked, in this case mixitup plugin. You click to filter your results, this adds display: none or display: inline-block to the appropriate containers.

Using this code from another stack question

$(function () {
    var $items = $($(".partners_list.container article.mix").get().reverse());

    $items.each(function () {
        if ($(this).css("display") != "none") {
            $(this).addClass("red");
            return false;
        }
    });
});

It works but only when the page first loads, after you active the mixitup and filter some results it doesn’t add the class red to the last ‘visible’ container i assume because its already loaded and done its job..

The mix it function is as follows..

$(function(){
    var $filterSelect = $('#FilterSelect'),
    $container = $('#partner_container');

    $container.mixItUp({
        animation: {
            enable: false       
        }
    });

    $filterSelect.on('change', function(){
        $container.mixItUp('filter', this.value);
    });           
});

So essentially need it to fire based on when the display: none and display:inline-block appears and disappears on the page.

Community
  • 1
  • 1
user4630
  • 633
  • 1
  • 9
  • 23

2 Answers2

0

So I think if you wrap the code you want to fire again in a function you can then set it to fire on callback from the mixItUp and on first load.

So you'd have a function like this:

function updateDisplay() {
    var $items = $($(".partners_list.container article.mix").get().reverse());

    $items.each(function () {
        if ($(this).css("display") != "none") {
            $(this).addClass("red");
            return false;
        }
    });
}

And then call it on first load like this:

$(function () {
    updateDisplay();
});

And then edit your mixItUp declaration to call this function also on callback of mixItUp having finished:

$container.mixItUp({
    animation: {
        enable: false       
    },
    callbacks: {
        onMixEnd: function(){
            alert('No items were found matching the selected filters.');
        }
    }
});

Hope that helps.

AndrewPolland
  • 3,041
  • 4
  • 28
  • 39
-1

Thanks to shodaburp i’ve managed to figure it out with the callback function, god knows how must be a fluke.

The full code i have now and seems to work...

 $(function(){

            var $filterSelect = $('#FilterSelect'),
                $container = $('#partner_container');

            $container.mixItUp({
                animation: {
                    enable: false       
                },

                callbacks: {
                        onMixEnd: function(state){


                            var $items = $($(".partners_list.container article.mix").get().reverse());

                                $items.each(function () {
                                    if ($(this).css("display") != "none") {
                                        $(this).addClass("red");
                                        return false;
                                    }
                                });

                        }
                    }

             });


            $filterSelect.on('change', function(){
              $container.mixItUp('filter', this.value);
            });


          });
user4630
  • 633
  • 1
  • 9
  • 23