10

I'm testing with IE8. I just upgraded jQuery from v1.5.2 to v1.6.1 and now the data method isn't working.

the row look like this:

<tr class="ui-widget-content alt" nodeIndex="2" data-DocAttributeFieldType="TextBox" data-DocClassAttributeFieldId="60777" jQuery16106588245076914028="66">

this works:

$("#docClassAttributeFields tbody tr:first").attr("data-DocClassAttributeFieldId");

this does not work:

$("#docClassAttributeFields tbody tr:first").data("DocClassAttributeFieldId");

Is there a bug in it?

Here is an example. Run it with in 1.5.2 and then 1.6 to see how they act differently... http://jsfiddle.net/5hbKX/

Homer
  • 7,594
  • 14
  • 69
  • 109

1 Answers1

16

From the docs (I suspect the change mentioned in 1.6 is to blame - have you tried removing the case, look at the lastValue example?):

HTML 5 data- Attributes

As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery's data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.

For example, given the following HTML:

<div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>

All of the following jQuery code will work.

$("div").data("role") === "page";
$("div").data("lastValue") === 43;
$("div").data("hidden") === true;
$("div").data("options").name === "John";

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string. To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method. When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names. The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).

From the above HTML5 specification:

A custom data attribute is an attribute in no namespace whose name starts with the string "data-", has at least one character after the hyphen, is XML-compatible, and contains no characters in the range U+0041 to U+005A (LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z).

All attributes on HTML elements in HTML documents get ASCII-lowercased automatically, so the restriction on ASCII uppercase letters doesn't affect such documents.

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
Orbling
  • 20,413
  • 3
  • 53
  • 64
  • I guess that's it. Changing to lowercase fixed it: http://jsfiddle.net/5hbKX/1/ That kind-of stinks, this is much less readable: .data("docclassattributefieldid") vs .data("DocClassAttributeFieldId") – Homer Jun 28 '11 at 16:54
  • Interesting side note, if you view the source in Firebug (FF) and Firebug-lite (IE) the data-* is lowercased, but viewing it in IE Developer Tools it is mixed case. – Homer Jun 28 '11 at 17:05
  • 6
    If captial letters are not allowed, use-hyphens-to-concatenate-words-instead. – Betamos Jun 28 '11 at 17:12
  • 2
    Ok, that adds to the confusion a little, capital letters work if there is a hyphen. So .data("id-D") works, .data("idB") does not... http://jsfiddle.net/5hbKX/2/ – Homer Jun 28 '11 at 17:42
  • Now I see... _3. For each name on the list, for each U+002D HYPHEN-MINUS character (-) in the name that is followed by a character in the range U+0061 to U+007A (U+0061 LATIN SMALL LETTER A to U+007A LATIN SMALL LETTER Z), remove the U+002D HYPHEN-MINUS character (-) and replace the character that followed it by the same character **converted to ASCII uppercase**._ – Homer Jun 28 '11 at 17:47
  • @Homer, if you put your values in to the HTML hyphenated, then you can pull them out camel-case if you wish. It is a bit annoying breaking backwards compatibility. – Orbling Jun 28 '11 at 21:32
  • Yeah, just glad it was an easy fix. Thanks! – Homer Jun 29 '11 at 15:14
  • Is there a way to get them with the hyphens intact? – Timo Huovinen Apr 03 '14 at 12:32
  • @TimoHuovinen: You can retrieve the full attribute set easily enough, which preserves the hyphenation as far as I can tell playing in Chrome. (See: http://stackoverflow.com/a/16935800/438971) - Then you could easily filter through for entries starting with `'data-'`. – Orbling Jun 02 '14 at 22:59
  • @Orbling thats exactly what I ended up doing. The standardized _inconsistencies_ are sometimes inconvenient and weird because they attempt to support legacy methods. – Timo Huovinen Jun 03 '14 at 07:11
  • "this is much less readable: .data("docclassattributefieldid") vs .data("DocClassAttributeFieldId")". How about .data("docClassAttributeFieldId")? – Helmut Granda Mar 08 '17 at 15:13