5

Is it expected that for

<div data-foo-42="bar"></div>

div the div.data() would be an empty object?

Demo: http://jsfiddle.net/nWCKt/

What are the requirements for data- attributes names?

Created a ticket in jquery bug tracker: http://bugs.jquery.com/ticket/14376

9
  • 1
    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). Commented Sep 20, 2013 at 4:16
  • 1
    Using data() you can access the attribute like so: div.data("foo-42") rather than div.attr("div-foo-42") - does that help? Commented Sep 20, 2013 at 4:18
  • @Arun P Johny: how my name violates that requirement? Commented Sep 20, 2013 at 4:18
  • it looks like a jQuery data issue... Commented Sep 20, 2013 at 4:18
  • 1
    @zerkms it looks to be a bug in the jQuery data method(in the dataAttr method) Commented Sep 20, 2013 at 4:39

1 Answer 1

4

Seems like a jquery issue to me. The regex that replaces key is causing the issue when there is a numeric value after a hyphen separator of the attribute (Other than the first hyphen for data-). They need a way to identify numeric values that start after the attribute on a multidash data attribute.

Snippet from jquery:

function dataAttr( elem, key, data ) {
    // If nothing was found internally, try to fetch any
    // data from the HTML5 data-* attribute
    if ( data === undefined && elem.nodeType === 1 ) {

        var name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase(); 

        data = elem.getAttribute( name );

         ....

and another one which converts data attrib name to the key used in the above place which actually converts the key to camelcase rdashAlpha = -([\da-z]), and the second replacement (.replace( rdashAlpha, fcamelCase );) considers a numeric value after a separator to be a part of the prev separator. This probably is the core culprit which ignores numeric start after second dash.

camelCase: function( string ) {
        return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
    },

Here key becomes foo42 and while replacing it with rmultiDash (which is /[A-Z]/) becomes foo42 so there is no attribute with the name data-foo42 it is data-foo-42 instead. I think they need to have some identifier (similar to capitalization of the key for the first char after hash) to represent starting numerals after the hash.

<div  data-foo-42="bar" data-foo-tfs="tf"></div>

returns {fooYui: "bar"} skips the first attribute.

<div data-foo-d42="bar" data-foo-YUI="bar"></div>

returns {fooD42: "bar", fooYui: "bar"}

using jquery 1.10.1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.