Skip to content

How to set data attributes in HTML elements


I have a div with an attribute data-myval = "10". I want to update its value; wouldn’t it change if I use'myval',20)? Do I need to use div.attr('data-myval','20') only?

Am I getting confused between HTML5 and jQuery? Please advise. Thanks!

Updated'myval')=20 to'myval',20), but still the HTML is not updating.


<div id="mydiv" data-myval="10"></div>


var a = $('#mydiv').data('myval'); //getter

$('#mydiv').data('myval',20); //setter

Posted in jquery. Tagged with , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

You must be logged in to post a comment.