You can loop through any element with a certain property and then change that element, like in the example below:
$('[data-name]').each(function (e) {
// Set the data-name to the item number
$(this).attr('data-name', e);
// Print the new `data-name` to the console
console.log($(this).attr('data-name'));
});
JSFiddle: http://jsfiddle.net/g2tpJ/5/
Note: Take a look at this post to decide on using attr
or data
:
jQuery Data vs Attr?