2

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();
});
2
  • You just need to add jQuery to your fiddle and the syntax $('div').removeClass(); is just fine... Commented Dec 10, 2014 at 8:42
  • check this jsfiddle.net/qxy7yotj/5 Commented Dec 10, 2014 at 8:43

3 Answers 3

2

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');
Sign up to request clarification or add additional context in comments.

1 Comment

@Igor...your update kind of changes the nature of the question!
1

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.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.