user1681836
user1681836

Reputation: 51

jQuery get .text and use as class

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

Answers (2)

user1681836
user1681836

Reputation: 51

$.each($('.main-amen-text'), function() {
    var text = $(this).html().replace(/\W+/g, '-').toLowerCase();
    $(this).addClass('main-amen-text-' + text);
});

Upvotes: 1

Amir Iqbal
Amir Iqbal

Reputation: 881

Try This

text.toLowerCase().replace(/ /g, '');

Upvotes: 0

Related Questions