Skip to main content

Bug Tracker

Side navigation

#9066 closed bug (invalid)

Opened May 03, 2011 04:58PM UTC

Closed May 03, 2011 05:17PM UTC

Last modified October 17, 2011 08:49AM UTC

$(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:
Blocked by: Blocking:
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.

Attachments (0)
Change History (10)

Changed May 03, 2011 05:08PM UTC by anonymous comment:1

Here is a fiddle demonstrating the issue...

http://jsfiddle.net/UvYdA/

Changed May 03, 2011 05:17PM UTC by rwaldron comment:2

_comment0: According to the spec, all html attributes are lowercased automatically. In fact... \ \ http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data-with-the-data-*-attributes1304443092051022
component: unfileddata
priority: undecidedlow
resolution: → invalid
status: newclosed

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)"

http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data-with-the-data-*-attributes

Changed May 03, 2011 05:19PM UTC by timmywil comment:3

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/

Changed May 03, 2011 05:40PM UTC by anonymous comment:4

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...

Changed May 03, 2011 05:48PM UTC by anonymous comment:5

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.

Changed May 03, 2011 06:15PM UTC by rwaldron comment:6

keywords: → needsdocs

Adding needsdocs tag

Changed May 03, 2011 06:53PM UTC by dmethvin comment:7

keywords: needsdocs

Updated docs.

Changed May 03, 2011 06:58PM UTC by dmethvin comment:8

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.

Changed May 03, 2011 07:06PM UTC by anonymous comment:9

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!

Changed October 17, 2011 08:49AM UTC by keith.kirkhope@googlemail.com comment:10

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.