Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve the case of a data attribute?

The html object:

 <div data-myAttribute="test"></div>

The code:

 var $o = $("div");

 $.each($o.data(),function(k,v){
    console.log(k); 
    //writes 'myattribute' instead of 'myAttribute'
 });

How do I preserve the case of the attribute?

like image 512
Control Freak Avatar asked Sep 11 '25 10:09

Control Freak


2 Answers

Valid HTML data attributes can't contain uppercase characters anyway:

From the W3:

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

like image 183
j08691 Avatar answered Sep 13 '25 01:09

j08691


You can't. Attribute names are always lowercase in HTML5.

like image 27
Scimonster Avatar answered Sep 13 '25 01:09

Scimonster