Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#9249 closed enhancement (wontfix)

data method called on a jQuery collection only returns the data value of the first item

Reported by: [email protected] Owned by:
Priority: low Milestone: 1.next
Component: data Version: 1.6
Keywords: Cc:
Blocked by: Blocking:


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 (2)

comment:1 Changed 12 years ago by ajpiano

Component: unfileddata
Priority: undecidedlow
Resolution: wontfix
Status: newclosed
Type: bugenhancement

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!

Last edited 12 years ago by ajpiano (previous) (diff)

comment:2 Changed 12 years ago by anonymous

fair enough - it'd be a shortcut at best anyway. marking incorrectly entered form fields would be a good example:

$('form input').data('validate')();
Note: See TracTickets for help on using tickets.