Reputation: 51
I'm trying to get the text content from a div and add it as an extra class, I have managed to figure out how to get it, but I can't understand how to make lowercase and remove spaces so it acts as a good class.
What I have so far;
<div class="main-amen-text">Apple</div>
<div class="main-amen-text">Something Fruity</div>
$.each($('.main-amen-text'), function () {
var text = $(this).html();
$(this).addClass('main-amen-text-' + text);
});
Upvotes: 1
Views: 43
Reputation: 51
$.each($('.main-amen-text'), function() {
var text = $(this).html().replace(/\W+/g, '-').toLowerCase();
$(this).addClass('main-amen-text-' + text);
});
Upvotes: 1