How to remove class from all child elements
34,647
Solution 1
A simple and elegant solution would be:
$(".class").removeClass("class");`
Solution 2
Instead of removeAttr('class')
you can use removeClass("classname")
If you want to remove all the class values from its children, then do like
$('#message').find("*").prop("class","");
or even
$('#message').find('*') removeAttr('class');
Here is a fiddle.
Solution 3
Older thread, but in my case I wanted to remove a specific class from all child elements
$('#parentDiv').find("*").removeClass("row-selected");
Solution 4
You need to use find to grab all the nested elements, then iterrate over them.
$('#message').find('*').each(function(){
$(this).removeAttr('class');
})
Related videos on Youtube
Comments
-
Shijin TR almost 2 years
I have a html like:
<table id="message"> <tr class="clsaa1"> <td> <div class="class3"> <table class="sub_1"> ---------------------- ---------------------- </table> <div> </td> </tr> <tr class="clsaa2"> <td> <div class="class3"> ---------------------- ---------------------- <div> </td> </tr> </table>
I need to remove all
class
attributes inside#message
.I have tried
$('#message').siblings().removeAttr('class');
and
$('#message').children().removeAttr('class');
But this is not working.
-
Shijin TR over 10 yearsI want to remove some other attributes in the same manner,Not only class.
-
Shijin TR over 10 yearsI dont know the class name also.Remove all class
-
shenku over 10 yearsthis wont work, you need to iterrate over the childrens children.
-
Shijin TR over 10 yearsI can't predict the class names
-
shenku over 10 years@shijin there you go updated my answer, let me know if it works.
-
Shijin TR over 10 yearsI got "SyntaxError: Unexpected token *" Error
-
Mithun Satheesh over 10 years@shijin : see my answer.
-
shenku over 10 years@shijin jus twrap the start in '*'
-
Shijin TR over 10 yearsWith $('#message').find('*') removeAttr('class'); is also working
-
clintgh about 9 yearsThis works, just had to change
removeAttr
toremoveClass
. -
Michael Fromin about 8 yearsThis is the simplest answer of them all to strip a class from an unknown set of elements!