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');
})
Share:
34,647

Related videos on Youtube

Shijin TR
Author by

Shijin TR

Updated on July 09, 2022

Comments

  • Shijin TR
    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
    Shijin TR over 10 years
    I want to remove some other attributes in the same manner,Not only class.
  • Shijin TR
    Shijin TR over 10 years
    I dont know the class name also.Remove all class
  • shenku
    shenku over 10 years
    this wont work, you need to iterrate over the childrens children.
  • Shijin TR
    Shijin TR over 10 years
    I can't predict the class names
  • shenku
    shenku over 10 years
    @shijin there you go updated my answer, let me know if it works.
  • Shijin TR
    Shijin TR over 10 years
    I got "SyntaxError: Unexpected token *" Error
  • Mithun Satheesh
    Mithun Satheesh over 10 years
    @shijin : see my answer.
  • shenku
    shenku over 10 years
    @shijin jus twrap the start in '*'
  • Shijin TR
    Shijin TR over 10 years
    With $('#message').find('*') removeAttr('class'); is also working
  • clintgh
    clintgh about 9 years
    This works, just had to change removeAttr to removeClass.
  • Michael Fromin
    Michael Fromin about 8 years
    This is the simplest answer of them all to strip a class from an unknown set of elements!

Related