jQuery .hasClass() method fails for SVG elements

css, html, javascript, jquery, svg

Solution

The class attribute for HTML element doesn't have the same meaning in SVG.

$("<b></b>").addClass($(this).attr("class")).hasClass("node")

Or

/(^|\s)node(\s|$)/.test($(this).attr("class"))

for SVG elements.

EDIT .hasClass seems to work just fine (at least in IE9 and FF) http://jsfiddle.net/X6BPX/1/

So the problem could be any combination of the following: a syntax error, using an outdated browser, using an outdated version of jQuery.

Problem

I have a set of SVG elements with the classes `node` and `link`. My program should detect whether an element has the `node` class or the `link` class upon hovering over any of the SVG elements. However, for some reason, the `.hasClass()` doesn't seem to work: ``` $(".node").hover(function(evt){ console.log($(this).attr("class")); //returns "node" console.log($(this).hasClass('node')); //returns false }, function(){console.log("Done");}); ``` So the element I hovered on has the class `node`, and jQuery detects that too, as shown by `console.log($(this).attr("class"));`, but for some reason the actual `.hasClass()` fails. Why is this? Is it failing because of the SVG?

Original source

Related problems