I want to add a data attribute to an element and dynamically give it the same value as the class name.
I used this script to do the same from the elements string, but instead I want to pass the value from the class name and don't know how to do it.
Script:
$('.newgroup').attr('data-price', function() {
var text = $(this).text();
return parseInt(text, 10);
});
HTML
<div class="newgroup">/div>
It should look like this:
<div class="newgroup" data-price="newgroup">>/div>
source
share