Reputation: 143
I have a library that generates an element with a specific class. I do not want to edit the library at all, how would I create a function with jquery to search for a specific class name and append another class after it?
ie: Lib creates:
<div class="class-lib">
Function searches for "class-lib" and adds class-func
<div class="class-lib class-func">
Upvotes: 1
Views: 108
Reputation: 18873
Try this:
$(".class-lib").each(function(){
$(this).addClass("class-func")
});
OR
$( "div.class-lib" ).addClass( "class-func" );
Upvotes: 0
Reputation: 756
What you're after is jQuery addClass.
Put simply:
$(".class-lib").addClass('class-func');
For more info see: http://api.jquery.com/addclass/
Upvotes: 0
Reputation: 11808
Here,function will find class "class-lib" and will add class "class-func" to it
$('body').find('.class-lib').addClass('class-func');
Upvotes: 0
Reputation: 2573
Here:
$( ".class-lib" ).addClass( "class-func" );
first part $( ".class-lib" )
gets reference to the element with class name class-lib
. Then the second part .addClass( "class-func" );
adds the class class-func
to it.
Upvotes: 1