jQuery 3 does not have this problem
One of the changes listed on the jQuery 3.0 revisions is:
add SVG class manipulation (#2199, 20aaed3)
One solution for this issue would be to upgrade to jQuery 3. It works great:
var flip = true;
setInterval(function() {
// Could use toggleClass, but demonstrating addClass.
if (flip) {
$('svg circle').addClass('green');
}
else {
$('svg circle').removeClass('green');
}
flip = !flip;
}, 1000);
svg circle {
fill: red;
stroke: black;
stroke-width: 5;
}
svg circle.green {
fill: green;
}
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<svg>
<circle cx="50" cy="50" r="25" />
</svg>
The Problem:
The reason the jQuery class manipulation functions do not work with the SVG elements is because jQuery versions prior to 3.0 use the className
property for these functions.
cur = elem.nodeType === 1 && ( elem.className ?
( " " + elem.className + " " ).replace( rclass, " " ) :
" "
);
This behaves as expected for HTML elements, but for SVG elements className
is a little different. For an SVG element, className
is not a string, but an instance of SVGAnimatedString
.
Consider the following code:
var test_div = document.getElementById('test-div');
var test_svg = document.getElementById('test-svg');
console.log(test_div.className);
console.log(test_svg.className);
#test-div {
width: 200px;
height: 50px;
background: blue;
}
<div class="test div" id="test-div"></div>
<svg width="200" height="50" viewBox="0 0 200 50">
<rect width="200" height="50" fill="green" class="test svg" id="test-svg" />
</svg>
If you run this code you will see something like the following in your developer console.
test div
SVGAnimatedString { baseVal="test svg", animVal="test svg"}
If we were to cast that SVGAnimatedString
object to a string as jQuery does, we would have [object SVGAnimatedString]
, which is where jQuery fails.
How the jQuery SVG plugin handles this:
The jQuery SVG plugin works around this by patching the relevant functions to add SVG support.
function getClassNames(elem) {
return (!$.svg.isSVGElem(elem) ? elem.className :
(elem.className ? elem.className.baseVal : elem.getAttribute('class'))) || '';
}
This function will detect if an element is an SVG element, and if it is it will use the baseVal
property of the SVGAnimatedString
object if available, before falling back on the class
attribute.
jQuery's historical stance on the issue:
jQuery currently lists this issue on their Won’t Fix page. Here is the relevant parts.
SVG/VML or Namespaced Elements Bugs
jQuery is primarily a library for the HTML DOM, so most problems related to SVG/VML documents or namespaced elements are out of scope. We do try to address problems that "bleed through" to HTML documents, such as events that bubble out of SVG.
Evidently jQuery considered full SVG support outside the scope of the jQuery core, and better suited for plugins.