function jqmajorver() {
return (jQuery && $().jquery || 0).split('.')[1] || 0;
}
function w3cdatakey(string) {
//transform data-attributes string like data-foo-bar to fooBar and ones like data-fooBar to foobar
//to comply with w3c HTML5 data- attributes specification, and changes to the way
//jQuery transforms data- attributes to camelcase and lowercase after version 1.6
var fixdashes = (jqmajorver() >= 6 ? true : false);
var newstring = '' + string; //copy of string
if (newstring.substr(0, 5) == 'data-') {
newstring = newstring.substr(5);
} //trim off leading 'data-' if provided
newstring = newstring.toLowerCase();
if ( !! fixdashes && newstring.match('-')) {
var parts = newstring.split('-'),
tmpstr = '';
$.each(parts, function(i, el) { //replace dashes and make letters to right of dashes capitals (camelcase)
tmpstr += (i === 0 ? el : el.charAt(0).toUpperCase() + el.slice(1)); //capitalize if not first item
});
newstring = tmpstr;
}
return newstring;
}
$(document).ready(function() {
$('#attsubmit').click(function(e) {
e.preventDefault();
var el = $('#dataattr'),
vall = el.val(),
res = $('#translation');
res.text(w3cdatakey(vall));
});
//onDomReady, get some data from data attributes already set
var trans = $('#translation'),
datadebug = '#translation data debugging: ',
keys = ['data-fooBar', 'data-foo-bar', 'FooBarBaz', 'data-fooBar-BazBo'];
$.each(keys, function(ii, ell) {
datadebug += ell + '[' + w3cdatakey(ell) + ']: ' + trans.data(w3cdatakey(ell)) + '; ';
});
trans.before(datadebug);
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.