Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
[1.x] .data() keys do not match HTML5 data specification when two dashes are used #2070
jQuery has provided the
In jQuery 2.x, this works correctly and
In jQuery 1.x, data attributes with two consecutive dashes (
In 1.6 through 1.6.3, attributes containing two dashes in a row are not included in the returned object. This appeared to be fixed in 8e8fa6d, which makes 1.6.4 the first version where this bug appears in 1.x.
HTML code used for testing
<div data-one="none" data-nested-single="single" data-nested--double="double"></div>
var data = $('div').data(); assert.equal(data['one'], 'none'); assert.equal(data['nestedSingle'], 'single'); assert.equal(data['nested-Double'], 'double');
This will pass in 2.x, but fail in 1.x.
var data = $('div').dataset; assert.equal(data['one'], 'none'); assert.equal(data['nestedSingle'], 'single'); assert.equal(data['nested-Double'], 'double');
This passes, as expected.
As we have established already, jQuery 1.x does not correctly handle `data-*` attributes where there are multiple dashes. This makes it so we can still handle nested options when working with jQuery 1.x by using the `.dataset` option that is supported by all major browsers as well as IE 11+. Browser support tables for the `.dataset` attributes can be found at http://caniuse.com/dataset A notice was already added to the documentation about this in caeb0ec. The related ticket in the jQuery repository about this issue is jquery/jquery#2070. This closes #2969.