Hi all. I had a method that determined if an element's parent had a certain 
class, but it was too slow. I've gone through several iterations to find the 
most efficient, but it seems like it can be sped up even more. What am I doing 
wrong? and/or how can I have the most efficient method? I've included 3 
versions here...

<script>
var App = {
    parentWithClassVersion1: function (id, className) {
        var el = document.id(id);
        if (!el) { return false; }
        return el.getParents().some(function (parent) {
            if (parent.hasClass(className)) { return true; }
        });
    },
    parentWithClassVersion2: function (id, className) {
        var el = document.id(id);
        if (!el) { return false; }
        return !!el.getParents('[class*="'+className+'"]').length;
    },
    parentWithClassVersion3: function (id, className) {
        var el = document.id(id);
        if (!el) { return false; }
        var parent = el.getParent();
        if (!parent) { return false; }
        if (parent.hasClass(className)) { return true; }
        return App.parentWithClassVersion3(parent, className);
    }
};
</script>

Any help would be much appreciated!

Thanks,
~Philip

Reply via email to