Mykyta Severyn
Mykyta Severyn

Reputation: 5

How to add title of element to class attribute using jquery

I have seen that is it possible to transfer title attribute to class. The question is how to find the elements of the first word (animated) and assign them to the class title?

For example:

<div class="colelem" title="animated infinite bounce"></div>
<div class="colelem" title="animated infinite bounce"></div>

Add the class from the tittle to happen

<div class="colelem animated infinite bounce" title="animated infinite bounce"></div>
<div class="colelem animated infinite bounce" title="animated infinite bounce"></div>

Upvotes: 0

Views: 2127

Answers (1)

Mohammad
Mohammad

Reputation: 21499

You can use .addClass( function ) to do this work.

$(".colelem").addClass(function(){
    return $(this).attr("title");
});
.animated { 
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="colelem" title="animated infinite bounce">A</div>
<div class="colelem" title="animated infinite bounce">B</div>
<div class="colelem" title="animated infinite bounce">C</div>

Upvotes: 2

Related Questions