Side navigation
#9249 closed enhancement (wontfix)
Opened May 12, 2011 06:20AM UTC
Closed May 12, 2011 08:10AM UTC
Last modified May 12, 2011 08:19AM UTC
data method called on a jQuery collection only returns the data value of the first item
Reported by: | matt@mattlohkamp.com | Owned by: | |
---|---|---|---|
Priority: | low | Milestone: | 1.next |
Component: | data | Version: | 1.6 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
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
Attachments (0)
Change History (2)
Changed May 12, 2011 08:10AM UTC by comment:1
_comment0: | 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. \ 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. 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 probably cannot entertain this proposal. → 1305187827739270 |
---|---|
_comment1: | 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. \ \ 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. 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 probably cannot entertain this proposal. → 1305187849468259 |
_comment2: | 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. 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 probably cannot entertain this proposal. → 1305187873574586 |
_comment3: | 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 probably cannot entertain this proposal. → 1305187893492146 |
component: | unfiled → data |
priority: | undecided → low |
resolution: | → wontfix |
status: | new → closed |
type: | bug → enhancement |
Changed May 12, 2011 08:19AM UTC by comment:2
fair enough - it'd be a shortcut at best anyway. marking incorrectly entered form fields would be a good example:
$('form input').data('validate')();
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!