> > I did do so, but I think it doesn't help to have functions which have > semantically irritating names. If a function is called exists(), that > indicates, that the function is operating on one or no element, not a > collection (the same problem as with is()). Oh I'm not saying exists() is the best possible name. I like it, yes, but if you have better ideas please share ; ). > They could also return an array with all > values of this attribute in the collection. Then $('a').attr('href')[0] would > be the same as the current attr() function. > > Another thing I dislike about attr() is that attr('name','value') operates on > all Elements of the collection while attr('name') doesn't. I think that is > irritating. > I very much agree with you here. All of the property accessing functions behave very unpredictable when used on a collection of items. You explained what attr() currently does. text() is even more confusing as it returns a string with the text of all matched elements combined. Having those functions return an array would be great, but I can see it causing problems with existing code. But returning the value directly if only one item was matched should fix most of that. Now that of course would also cry for a way to enforce the array returning behavior consistently (even if just one element is matched). I'd suggest the following for that:
$.array = function(a){return a.constructor == Array ? a : [a];} $('a').attr('href') // Returns the href if only one element is matched or an array of hrefs if several elements exist $.array($('a').attr('href')) // Always returns an array of hrefs, even if just one element matches the selector > To make that complete I also think, that is() should better return an array > of > boolean values that indicate for every element in the jQuery object if it > fits to the selector. If only one of then is sufficient, then I'd call that > function e.g. has(): Another point I agree with you, 'has' does seem like a better name for operating on a set of elements. Would be nice to hear some of the devs comment on this, but I guess we are on the wrong list with this discussion as there also is a dev list I think. -- Felix -------------------------- My Blog: http://www.thinkingphp.org My Business: http://www.fg-webdesign.de Christof Donat wrote: > Hi, > > >> Christof: I appreciate your comments. Even more however I would have >> appreciated if you'd have taken into consideration why I made the >> proposal. >> > > I did do so, but I think it doesn't help to have functions which have > semantically irritating names. If a function is called exists(), that > indicates, that the function is operating on one or no element, not a > collection (the same problem as with is()). That will make the way to learn > jQuery even harder because people don't understand what they are working > with. > > If you are working with a saw, then break() is a senseless operation even > when > it is often successfully used on flintstones. If people expect the saw to be > used like a flintstone, they will fail, even if the saw has an operation > break() that tries to guess what a flintstone user is trying to do. It is > better to educate flintstone users how to use a saw and what can be done with > it. > > >> I just think >> that jQuery has gained a lot of it's popularity due to it's >> easy-to-learn factor (besides it's superiority and beauty that is : ) >> and think this could be a good addition to it. >> > > Well, I have not started using jQuery because it is easy to learn, but > because > it solved some problems for me. I quickly learned that my code can be more > elegand and slim when I invested my time to find the best ways of using > jQuery. > > >>> <div class="myClass">...</div> >>> <div class="myOtherClass">...</div> >>> >>> $('div').hasClass('myClass'); >>> >>> What should this return now? >>> >> It should return true. That's because $('a').attr('href') returns the >> 'href' attribute of the first anchor matched (afaik). >> > > Well, is() returns if there is one element in the collection that fits to the > selector. As we see, the semantics is unclear here. I am not really happy > with functions like attr() as well. They could also return an array with all > values of this attribute in the collection. Then $('a').attr('href')[0] would > be the same as the current attr() function. > > Another thing I dislike about attr() is that attr('name','value') operates on > all Elements of the collection while attr('name') doesn't. I think that is > irritating. > > To make that complete I also think, that is() should better return an array > of > boolean values that indicate for every element in the jQuery object if it > fits to the selector. If only one of then is sufficient, then I'd call that > function e.g. has(): > > $.fn.has = function(s) { > var r = false; > $.each(this.is(s),function(i,n) { > r |= n; > }); > return r; > }; > > Christof > >