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?
In order to remove SVG content, you can use the remove() function provided by D3. js.
parentElement could be for example $('g:first', svgRoot), where svgRoot refers to the embedded SVG element. rect. parentNode. removeChild(rect);
jQuery removeClass() Method The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.
To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.
You can't call .removeClass()
on a SVG, unfortunately.
Use .attr() instead.
$('.someClass').attr('class','');
additional sources:
Martin Wolf
Similar SO Question
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With