I am trying to set a data attribute to a div which previously doesn't have any value.
<div></div>
with jQuery, the first method (data) doesn't do anything, however the attr method works correctly.
var div = $('div');
div.data('superhero_one','batman');
div.attr('data-superhero_two','spiderman');
Am I using incorrectly the data function?
It does work !
The actual thing behind that is , you can't see actual attribute when you use .data()
because jquery sets that internally.
but .attr()
adds an attribute to the element which you can inspect.
see this demo -->
http://jsfiddle.net/5bT8p/1/