Jquery search for a class then add one after it

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

Answers (5)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

Try this:

$(".class-lib").each(function(){
  $(this).addClass("class-func")
});

OR

$( "div.class-lib" ).addClass( "class-func" );

Upvotes: 0

Allan W Smith
Allan W Smith

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

Domain
Domain

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

Amit Kumar
Amit Kumar

Reputation: 5962

$('div.class-lib').addClass('class-func');

DEMO

Upvotes: 0

Abdul Jabbar
Abdul Jabbar

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

Related Questions