17

here is my code

<!DOCTYPE html>
<html>
<body>
<style>    
   .someClass {
            fill:gray;
        }
</style>
<svg width="400" height="110">
  <rect class="someClass" width="100" height="100"/>
</svg>
 <svg width="400" height="110">
  <rect class="someClass" width="100" height="100"/>
</svg>
 <svg width="400" height="110">
  <rect class="a" width="100" height="100"/>
</svg>
</body>
</html>

How to remove all classes(.someClass) need to remove only in .someClass in document without using id?

Josh Stevenson
  • 895
  • 5
  • 17
Akbar Basha
  • 1,168
  • 1
  • 16
  • 38

4 Answers4

20

You can't call .removeClass() on a SVG, unfortunately.

Use .attr() instead.

$('.someClass').attr('class','');

additional sources:

Martin Wolf

Similar SO Question

Community
  • 1
  • 1
Josh Stevenson
  • 895
  • 5
  • 17
  • 8
    That will, of course, remove **all** classes. Which is fine for the quoted HTML, but... – T.J. Crowder Apr 20 '15 at 12:41
  • You could use the [form of `attr` that takes a function](http://api.jquery.com/attr/#attr-attributeName-function) as the second argument. This would give you the old value and you could do a `replaceAll()` to remove it. – GJK Apr 20 '15 at 20:10
  • Just wanted to add to this there is currently a [PR in jquery to change removeClass so this won't be an issue.](https://github.com/jquery/jquery/pull/2465) – aug Sep 18 '15 at 16:53
7

This ( $('.someClass').attr('class',''); ) will delete all the classes of all elements with class someClass.


This should do the trick ( retains other classes):

$(".someClass").each(function(){

     var classes = $(this).attr("class").split(" ");
     var otherClasses = "";
     for(var i = 0; i<classes.length; ++i){
        if(classes[i] !== "someClass"){
            otherClasses += classes[i] + " ";
        }
     }

     $(this).attr("class",otherClasses);

});

Thanks @xehpuk for his comments.

Matt
  • 74,352
  • 26
  • 153
  • 180
dsharew
  • 10,377
  • 6
  • 49
  • 75
4

You can use removeAttr, which will find all the matched elements and will remove the given attribute.
In your case this code will find all elements with class someClass and will remove the attribute class.
And the good in this code is that it will not leave an empty attribute.

$('.someClass').removeAttr( "class" );
chriz
  • 1,339
  • 2
  • 16
  • 32
0

You can use this. For more info see this https://stackoverflow.com/a/2156090/909535

rect.classList.remove("CLASS_NAME");
Community
  • 1
  • 1
meteor
  • 2,518
  • 4
  • 38
  • 52