× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Unable to set data attribute using jQuery Data() API

Leniel Macaferi
Feb 02, 2015
<p>I was having serious problems with</p> <pre><code>.data('property', value); </code></pre> <p>It was not setting the <code>data-property</code> attribute.</p> <p>Started using jQuery's <a href="http://api.jquery.com/attr/" rel="nofollow"><code>.attr()</code></a>:</p> <blockquote> <p>Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.</p> </blockquote> <pre><code>.attr('property', value) </code></pre> <p>to set the value and</p> <pre><code>.attr('property') </code></pre> <p>to retrieve the value.</p> <p>Now it just works!</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/6827810/Unable%20to%20set%20data%20attribute%20using%20jQuery%20Data()%20API/26004735">Stack Overflow</a>.</p>
comments powered by Disqus