Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is mentioned in the <a href="http://api.jquery.com/data" rel="nofollow noreferrer"><code>.data()</code> documentation</a></p> <blockquote> <p>The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery)</p> </blockquote> <p>This was also covered on <a href="https://stackoverflow.com/questions/5507718/why-dont-changes-to-jquery-fn-data-update-the-corresponding-html-5-data-at/5507828#5507828">Why don&#39;t changes to jQuery $.fn.data() update the corresponding html 5 data-* attributes?</a></p> <p>The demo on my original answer below doesn't seem to work any more.</p> <p><strong>Updated answer</strong></p> <p>Again, from the <a href="http://api.jquery.com/data" rel="nofollow noreferrer"><code>.data()</code> documentation</a></p> <blockquote> <p>The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.</p> </blockquote> <p>So for <code>&lt;div data-role="page"&gt;&lt;/div&gt;</code> the following is true <code>$('div').data('role') === 'page'</code></p> <p>I'm fairly sure that <code>$('div').data('data-role')</code> worked in the past but that doesn't seem to be the case any more. I've created a better showcase which logs to HTML rather than having to open up the Console and added an additional example of the multi-hyphen to camelCase <a href="http://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes" rel="nofollow noreferrer">data- attributes</a> conversion.</p> <p><a href="http://jsfiddle.net/nwh9ottk/4/" rel="nofollow noreferrer"><strong>Updated demo (2015-07-25)</strong></a></p> <p>Also see <a href="https://stackoverflow.com/questions/7261619/jquery-data-vs-attr">jQuery Data vs Attr?</a></p> <p><strong>HTML</strong></p> <pre><code>&lt;div id="changeMe" data-key="luke" data-another-key="vader"&gt;&lt;/div&gt; &lt;a href="#" id="changeData"&gt;&lt;/a&gt; &lt;table id="log"&gt; &lt;tr&gt;&lt;th&gt;Setter&lt;/th&gt;&lt;th&gt;Getter&lt;/th&gt;&lt;th&gt;Result of calling getter&lt;/th&gt;&lt;th&gt;Notes&lt;/th&gt;&lt;/tr&gt; &lt;/table&gt; </code></pre> <p><strong>JavaScript</strong> (jQuery 1.6.2+)</p> <pre><code>var $changeMe = $('#changeMe'); var $log = $('#log'); var logger; (logger = function(setter, getter, note) { note = note || ''; eval('$changeMe' + setter); var result = eval('$changeMe' + getter); $log.append('&lt;tr&gt;&lt;td&gt;&lt;code&gt;' + setter + '&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;' + getter + '&lt;/code&gt;&lt;/td&gt;&lt;td&gt;' + result + '&lt;/td&gt;&lt;td&gt;' + note + '&lt;/td&gt;&lt;/tr&gt;'); })('', ".data('key')", "Initial value"); $('#changeData').click(function() { // set data-key to new value logger(".data('key', 'leia')", ".data('key')", "expect leia on jQuery node object but DOM stays as luke"); // try and set data-key via .attr and get via some methods logger(".attr('data-key', 'yoda')", ".data('key')", "expect leia (still) on jQuery object but DOM now yoda"); logger("", ".attr('key')", "expect undefined (no attr &lt;code&gt;key&lt;/code&gt;)"); logger("", ".attr('data-key')", "expect yoda in DOM and on jQuery object"); // bonus points logger('', ".data('data-key')", "expect undefined (cannot get via this method)"); logger(".data('anotherKey')", ".data('anotherKey')", "jQuery 1.6+ get multi hyphen &lt;code&gt;data-another-key&lt;/code&gt;"); logger(".data('another-key')", ".data('another-key')", "jQuery &lt; 1.6 get multi hyphen &lt;code&gt;data-another-key&lt;/code&gt; (also supported in jQuery 1.6+)"); return false; }); $('#changeData').click(); </code></pre> <hr> <p><a href="http://jsfiddle.net/R3LP9/1/" rel="nofollow noreferrer"><strong>Older demo</strong></a></p> <hr> <p><strong>Original answer</strong></p> <p>For this HTML:</p> <pre><code>&lt;div id="foo" data-helptext="bar"&gt;&lt;/div&gt; &lt;a href="#" id="changeData"&gt;change data value&lt;/a&gt; </code></pre> <p>and this JavaScript (with jQuery 1.6.2)</p> <pre><code>console.log($('#foo').data('helptext')); $('#changeData').click(function() { $('#foo').data('helptext', 'Testing 123'); // $('#foo').attr('data-helptext', 'Testing 123'); console.log($('#foo').data('data-helptext')); return false; }); </code></pre> <p><a href="http://jsfiddle.net/XFgXd/3/" rel="nofollow noreferrer"><strong>See demo</strong></a></p> <p>Using the <a href="https://developers.google.com/chrome-developer-tools/" rel="nofollow noreferrer">Chrome DevTools</a> <em>Console</em> to inspect the DOM, the <code>$('#foo').data('helptext', 'Testing 123');</code> <strong>does not</strong> update the value as seen in the <em>Console</em> but <code>$('#foo').attr('data-helptext', 'Testing 123');</code> does.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload