This won't work. I'm not just looking for the immediate parent - I'm looking for any parent/grandparent/great grandparent/etc that may have that class.
Thx. ~Philip Sent from my iPhone On Jan 8, 2011, at 3:38 PM, Arian Stolwijk <[email protected]> wrote: > just use: > > var parent = el.getParent(); > return parent.hasClass('foo'); > > Or another nice one using reverse combinators: > > $('test').getElement('!> .foo').setStyle('background', 'red'); > http://www.jsfiddle.net/S2kGa/ > > Btw: why are you using the return App.parentWithClassVersoin3(parent, > className); seems like some unwanted recursion. > > > On Sat, Jan 8, 2011 at 10:26 PM, Philip Thompson <[email protected]> > wrote: > 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 >
