Nimatullah Razmjo
Nimatullah Razmjo

Reputation: 1961

JQuery change class name inside of specific elements

I am looking for JQuery script to change a tag class inside of specific tags.

For example,

<h4 class=" id_1">
    <span class="favorite"> </span>
</h4>

In above tags, I want to change class="favorite" to class="title" by id_1 class. CSS for favorite and title is different.

Thank you!

Upvotes: 0

Views: 1506

Answers (4)

Maruthi Gongadi
Maruthi Gongadi

Reputation: 67

$('.id_1 .favorite').removeClass('favourite').addClass('title');

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can use addClass and removeClass to achieve this:

$('.id_1 .favorite').removeClass('favorite').addClass('title');

You could also use toggleClass if you can guarantee the starting state of the relevant elements:

$('.id_1 .favorite').toggleClass('favorite title');

Upvotes: 0

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You can also use attr method:

$('.id_1 .favorite').attr('class','title');

Using JavaScript:

document.querySelector('.id_1 .favorite').setAttribute('class','title');

Upvotes: 1

user5570620
user5570620

Reputation:

You can use addClass and removeClass:

$(".id_1").removeClass("favourite").addClass("title");

Upvotes: 1

Related Questions