Igor
Igor

Reputation: 804

How to recursively remove CSS classes

I am trying to recursively remove all CSS classes from DIVs using jQuery.

this is what I've got so far, but it simply does not work

http://jsfiddle.net/qxy7yotj/4/ (UPDATED)

//UPDATE: another problem is that I have HTML as a JAvascript String and I have to manipulate with it this way

HTML:

<div class="c1">
  <div class="c1">
    <div class="c3">some text
      <div>blah blah</div>
    </div>
  </div>
</div>

JQuery:

$('div').removeClass();

$('div').each(function( index ) {
    $(this).removeClass();
});

Upvotes: 2

Views: 2436

Answers (3)

dashtinejad
dashtinejad

Reputation: 6253

First, create an element on the fly, then search inside it:

var s = '<div class="c1"> <div class="c1"><div class="c3">some text<div>blah blah</div></div></div></div>'

var $elem =
    // create on the fly element
    $('<div></div>')

        // put your string to its content
        .html(s)

        // find `div` elements
        .find('div')

            // remove `class` attribute
            .removeAttr('class');

$('#id').html($elem.html());

JSFiddle Demo.

Upvotes: 1

SW4
SW4

Reputation: 71170

You need to add jQuery to your fiddle, as this is the syntax you are writing your Javascript in.

Then you simply need to use:

$('div').removeAttr('class');

Upvotes: 2

kapantzak
kapantzak

Reputation: 11750

Use removeAttr('class') and load jQuery in your Fiddle check DEMO

$('div').each(function() {
   $(this).removeAttr('class');
});

Upvotes: 1

Related Questions