I am using jQuery SVG. I can't add or remove a class to an object. Anyone know my mistake?
The SVG:
<rect class="jimmy" id="p5" x="200" y="200" width="100" height="100" />
The jQuery that won't add the class:
$(".jimmy").click(function() {
$(this).addClass("clicked");
});
I know the SVG and jQuery are working together fine because I can target the object and fire an alert when it's clicked:
$(".jimmy").click(function() {
alert('Handler for .click() called.');
});
Here is my rather inelegant but working code that deals with the following issues (without any dependencies):
classList
not existing on<svg>
elements in IEclassName
not representing theclass
attribute on<svg>
elements in IEgetAttribute()
andsetAttribute()
implementationsIt uses
classList
where possible.Code:
Example usage:
Or just use old-school DOM methods when JQ has a monkey in the middle somewhere.
Learn the DOM people. Even in 2016's framework-palooza it helps quite regularly. Also, if you ever hear someone compare the DOM to assembly, kick them for me.
Based on above answers I created the following API