Editing data attributes This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) What if we only do reading of data-attributes. Since most are commming generated from databases.. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <div id="mydiv"></div> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var div = document.getElementById('mydiv'); var $div = $(div); var Data = function() { var warehouse = {}; var count = 1; return { reset: function() { count = 1; warehouse = {}; }, set: function (dom, data) { if (!dom.__data) { dom.__data = "hello" + count++; } warehouse[dom.__data] = data; }, get: function(dom) { return warehouse[dom.__data]; } }; }(); Data.set(div, {po: 'bla bla ', ta: '123456789', to: 'The red chick jumps over the white chicke'}); div.setAttribute('data-po', 'bla bla'); div.setAttribute('data-ta', '123456789'); div.setAttribute('data-to', 'The red chick jumps over the white chicken'); Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var a = div.getAttribute('data-po'), b = div.getAttribute('data-ta'), c = div.getAttribute('data-to'); Test 2 Title Async (check if this is an asynchronous test) Code var data = Data.get(div), a = data.po, b = data.ta, c = data.to; Test 3 Title Async (check if this is an asynchronous test) Code var data = div.dataset, a = data.po, b = data.ta, c = data.to; Test 4 Title Async (check if this is an asynchronous test) Code var data = $div.data(), a = data.po, b = data.ta, c = data.to; Test 5 Title Async (check if this is an asynchronous test) Code mydiv = $div.get(); var a = mydiv ['data-po']; var b = mydiv ['data-ta']; var c = mydiv ['data-to']; Test 6 Title Async (check if this is an asynchronous test) Code var data = $.data(div), a = data.po, b = data.ta, c = data.to; Test 7 Title Async (check if this is an asynchronous test) Code var a = $div.attr('data-po'), b = $div.attr('data-ta'), c = $div.attr('data-to');