Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot call method 'split' of undefined

Tags:

jquery

I'm trying to include on my website the Quicksand script, but I failed badly.
Firebug gives me this error: 65 Uncaught TypeError: Cannot call method 'split' of undefined:

for this script:

jQuery.noConflict();
jQuery(document).ready(function($){
    // Clone applications to get a second collection
    var $data = $("#portfolio-items").clone();

    //NOTE: Only filter on the main portfolio page, not on the subcategory pages
    $('#portfolio-terms ul li').click(function(e) {
        $("ul li").removeClass("active");   
        // Use the last category class as the category to filter by. This means that multiple categories are not supported (yet)
        var filterClass=$(this).attr('class').split(' ').slice(-1)[0];
jquery.custom.js:65 Uncaught TypeError: Cannot call method 'split' of undefined (repeated 6 times)

        if (filterClass == '.all current') {
            var $filteredData = $data.find('#portfolio-');
        } else {
            var $filteredData = $data.find('#portfolio-[data-type=' + filterClass + ']');
        }
        $("#portfolio-items").quicksand($filteredData, {
            duration: 800,
            easing: 'swing',
        });     
        $(this).addClass("active");             
        return false;
    });
});


See here: http://stakk.it/
what is the error?

thank you and sorry for my bad english!

like image 274
humanbeing Avatar asked Nov 02 '11 21:11

humanbeing


1 Answers

If .attr("class") returns undefined, you can't call .split on it because .split is a method of the String object and can't be called on undefined. You need to store the result of .attr("class") and then only split it if it is not undefined.

var filterClass = $(this).attr('class');
filterClass = filterClass ? filterClass.split(' ').slice(-1)[0] : '';

now filterClass will contain what you expect, or an empty string.

Edit: you could replace $(this).attr('class') with this.className, pulled from removed answer.

like image 155
Kevin B Avatar answered Jan 04 '23 21:01

Kevin B