Adding data attribute to DOM

Use the .data() method:

$('div').data('info', '222');

Note that this doesn’t create an actual data-info attribute. If you need to create the attribute, use .attr():

$('div').attr('data-info', '222');

Leave a Comment