1

Need a bit of help with a Js error I am getting please:

Uncaught TypeError: $portfolio.isotope is not a function

  
//ISOTOPE FUNCTION - FILTER PORTFOLIO FUNCTION

    $portfolio = $('.portfolio-items');
    $portfolio.isotope({
        itemSelector : 'li',
        layoutMode : 'fitRows'
    });
    $portfolio_selectors = $('.portfolio-filter >li>a');
    $portfolio_selectors.on('click', function(){
        $portfolio_selectors.removeClass('active');
        $(this).addClass('active');
        var selector = $(this).attr('data-filter');
        $portfolio.isotope({ filter: selector });
        return false;
    });
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
user2202463
  • 135
  • 1
  • 2
  • 12

1 Answers1

4

If you don't want your script to run on pages that don't contain the required elements (.portfolio-items), you can run your script conditionally based on the length property of your element collection stored in $portfolio:

$portfolio = $('.portfolio-items');

if ($portfolio.length) { // if 'length' is non zero. Enter block...

    $portfolio.isotope({
        itemSelector : 'li',
        layoutMode : 'fitRows'
    });
    $portfolio_selectors = $('.portfolio-filter >li>a');
    $portfolio_selectors.on('click', function(){
        $portfolio_selectors.removeClass('active');
        $(this).addClass('active');
        var selector = $(this).attr('data-filter');
        $portfolio.isotope({ filter: selector });
        return false;
    });

}
Turnip
  • 35,836
  • 15
  • 89
  • 111
  • I am facing the same issue, tried your solution but still having the same error. What could be other possible reasons for this error? – Sachin Vairagi Sep 12 '18 at 09:50
  • @SachinVairagi You would also get the `Uncaught TypeError: $portfolio.isotope is not a function` error if you haven't loaded the isotope library correctly. Check the Network section of your browser's developer tools. – Turnip Sep 12 '18 at 16:14
  • Thank you for your response, I have raised new question and posted my codes as well, can you please check - https://stackoverflow.com/questions/52292697/typeerror-container-isotope-is-not-a-function – Sachin Vairagi Sep 14 '18 at 07:10