Ticket #9066 (closed bug: invalid)
$(element).data() breaks with camel case "data-" attributes
| Reported by: | anonymous | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | data | Version: | 1.6 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
It appears that the changes to $(element).data() have broken some existing functionality.
If I have an element, <input id="foo" data-someThing="bar">, and try to access the data via $('foo').data('someThing') it's now returning -undefined- instead of "bar". However, $('foo').data('something') does return "bar".
I can't tell if this is a bug or not from the documentation, but it appears wrong to me. I'm assuming this broke/changed with the other changes to data regarding hyphens, but to me it shouldn't be modifying case of the data attribute when it does not contain any hyphens. Or, at a minimum, the documentation needs to be clear that this will happen.
Change History
comment:2 Changed 2 years ago by rwaldron
- Priority changed from undecided to low
- Resolution set to invalid
- Status changed from new to closed
- Component changed from unfiled to data
According to the latest Element data-* attributes spec, all html attributes are lowercased automatically. In fact, capitol letters are forbidden:
"contains no characters in the range U+0041 to U+005A (LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z)"
comment:3 Changed 2 years ago by timmywil
To be clear, we made this change in 1.6 to match the spec. To retrieve the data, use .data('something') and it will match. http://jsfiddle.net/timmywil/b2wgk/
comment:4 Changed 2 years ago by anonymous
This still seems wrong though considering if instead I use <input id="foo" data-some-thing="bar"> then this, $('#foo').data('someThing') is now valid.
This seems terribly inconsistent...
comment:5 Changed 2 years ago by anonymous
At a minimum http://api.jquery.com/data/#data2 should be updated to include these rules. For someone new to jQuery this would be extremely confusing.
comment:8 Changed 2 years ago by dmethvin
To be clear, the blog entry for 1.6 mentions this under Breaking Changes. And the reason it broke is because we fixed a bug in order to be in conformance with the standard. So it's unfortunate, and we're sorry, but it's the right move. Trying to make old code work would cause a bigger mess.
comment:9 Changed 2 years ago by anonymous
Fair enough, I did see the entry in the blog but wanted to make sure some note of it also made it out to the documentation.
Thanks everyone!
comment:10 Changed 19 months ago by keith.kirkhope@…
where in the http://api.jquery.com/data/ page does it say that <div data-roleOne="page" /> is only retrievable by calling $('div').data('roleone') ? If it does then its not clear and the "HTML 5 data- Attributes" should have an example showing it.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Here is a fiddle demonstrating the issue...
http://jsfiddle.net/UvYdA/