> Felix, not to worry, there's nothing wrong at all with using .length > - and it is obviously faster than a function call. I figured that by now. I think Matt was much better at explaining why I think an alternative exists() function is useful - it simply is the most intuitive thing a new jQuery user looks for. I also agree with his hasClass argument. I love the is() function, but I would have taken me a long time to find it if I my question was phrased "Who can I determine if an element has a certain class".is() is more powerful but not nearly as intuitive as hasClass would be for new users. > In the earliest versions of jQuery, the jQuery object was not an > array, but had a private array object that you accessed using .get(n) > and .size(). The only reason those functions still exist is for > compatibility with old code. > > There's no reason at all to stick with .get(n) and .size() now that > the array-like jQuery object allows the simpler and more efficient [n] > and .length. I disagree. Whenever you need to sort the elements in an <ul> or something then you'll have to use the Array.sort() function, so you need to do $('ul li').get().sort(...). Here is an example of where I needed this functionality: http://bin.cakephp.org/view/1632218532
It's not a big deal that I have to call get(), but it would be a big problem if it wasn't there! -- Felix -------------------------- My Blog: http://www.thinkingphp.org My Business: http://www.fg-webdesign.de Michael Geary wrote: > Felix, not to worry, there's nothing wrong at all with using .length > - and it is obviously faster than a function call. > > In the earliest versions of jQuery, the jQuery object was not an > array, but had a private array object that you accessed using .get(n) > and .size(). The only reason those functions still exist is for > compatibility with old code. > > There's no reason at all to stick with .get(n) and .size() now that > the array-like jQuery object allows the simpler and more efficient [n] > and .length. > > -Mike > > ------------------------------------------------------------------------ > *From:* Felix Geisendörfer > Sean, Mike: I agree with your notion that learning that the jQuery > object is array-like (It'd be cool if it was a real array and > .push / .sort would work on it) is very worthwhile. I knew that > when I initially stumbled across the problem and knew that doing > it via '.length' was one solution. I was only confused if it was > the right way or could lead to undesired results as I was new to > jQuery at this point and I didn't find it mentioned anywhere in > specific. That's where my notion of an 'exists()' function could > be useful comes from. But it's probably just something that should > be put in the manual somewhere (if it isn't already in there and I > missed it). > > -- Felix > -------------------------- > My Blog: http://www.thinkingphp.org > My Business: http://www.fg-webdesign.de > > > Sean Catchpole wrote: >> >> I believe that learning jquery returns an array like object is more >> useful than creating a .exists() function. >> >> ~Sean >>