api.jquery.com icon indicating copy to clipboard operation
api.jquery.com copied to clipboard

jQuery.fn.data() and data-* attribute name restrictions

Open rwaldron opened this issue 12 years ago • 6 comments

http://bugs.jquery.com/ticket/14376

jQuery.fn.data() will not work data-* attributes in the very specific case where an attribute has been named with multiple words and any of the words after the first word (not including data-) begin with a digit.

Example:

<div data-foo-42></div>

However, these work just fine:

<div data-42></div>
<div data-foo42></div>

rwaldron avatar Nov 06 '13 16:11 rwaldron

What do you mean by "will not work"? It appears to work fine here: http://jsbin.com/IzIKInET/1/edit?html,js,output

ithcy avatar Nov 06 '13 21:11 ithcy

What I mean by "will not work" is "will not work". Yes, you're asking for the property specifically, that's fine, we'll make sure the docs reflect this; in the meantime, take a look at this: http://jsbin.com/aWaWegi/4/edit?html,js,console

rwaldron avatar Nov 06 '13 22:11 rwaldron

Understood. I was just asking for clarification. It seemed to be "will not work except when it does work" based on my stupid test :)

ithcy avatar Nov 06 '13 22:11 ithcy

It seemed to be "will not work except when it does work" based on my stupid test :)

Unfortunately, yes. This is a particularly frustrating and hairy bug. @gnarf and I would ultimately like to break backward compatibility in favor of HTML5 data-* compatibility, but no definite answer yet.

rwaldron avatar Nov 07 '13 13:11 rwaldron

Backward compat is already broken.

$('<div data-mike-123="alsup"></div>').data('mike-123')

Works in 1.9, fails in 2.x.

I agree on the sentiment that it should track the dataset spec.

malsup avatar Dec 18 '13 15:12 malsup

This will be fixed in 3.0. However, we should document that retrieving data with keys containing numbers after dashes may not return the expected result before jQuery 3.0 and that the dash will not be removed in this case in 3.0+, which will reflect the dataset spec.

timmywil avatar May 04 '15 13:05 timmywil