David
David

Reputation: 16726

How to change class of all children with jquery?

I have the following syntax in which I want to remove all css classes from the link tags. How can I do this using jquery?

<div id="nav">
<ul>
<li><a href="" class="foo">aa</a></li>
<li><a href="" class="bar">aa</a></li>
<li><a href="" class="yay">aa</a></li>
</ul>
</div>

Upvotes: 3

Views: 19685

Answers (4)

draconis
draconis

Reputation: 458

You can use $("a").removeClass();

here is example: http://jsfiddle.net/UPsEK/

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101604

$('#nav ul li a').each(function(){
    this.className = '';
});

Finds all anchors, based on div#nav selector, and removes all class names.

Upvotes: 5

thelastshadow
thelastshadow

Reputation: 3644

If you want to remove every class then it's this:

$('#nav a').each(function(){
    $(this).removeAttr('class');
});

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318508

You can use the removeClass() method; if you call it without an argument it removes all classes:

$('#nav ul a').removeClass();

Description: Remove a single class, multiple classes, or all classes from each element in the set of matched elements.

Obviously you can chain .addClass('someClass') in the same call; you asked to change the class in your question's subject after all. ;)

Upvotes: 19

Related Questions