Ticket #9249 (closed enhancement: wontfix)
data method called on a jQuery collection only returns the data value of the first item
| Reported by: | matt@… | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | data | Version: | 1.6 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
In short, if you run the data storage method on a jquery collection of elements (eg $('p').data('foo','bar') ) it will apply that data to each of them.
However, if you run the data retrieval method on a jquery collection of elements (eh $('p').data('foo') ) it will only return the value of the first.
see example here - http://jsfiddle.net/3eERV/
... on the one hand, I can see the logic in this, but on the other, it breaks the convention of jQuery methods applying to each element in the collection.
Instead of the current behavior, I would expect it to return something like an array of values, compiled out of the value of each element in the collection.
thanks -
- m a t t
Change History
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Thanks for taking the time to write this up and contribute! I don't think this proposal really has legs for several reasons:
1) What are the semantics if there is only one element? I get an array with one item in it and then I have to access that array no matter what? I don't get an array, but only if there is one element?
2) It would break a lot existing code in ways that we can't provide backward compatibilty for.
3) Most importantly, it doesn't actually break any jQuery conventions. There are a number of methods that are overloaded as getters and setters - css, val, attr, data, etc. All of these work the exact same way - when used to set a value, they apply to the entire collection, and when used to get a value, they return the value from the first element in the collection.
I hope this explains why we cannot entertain this proposal. Thanks again!