Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .hasClass() method fails for SVG elements

Tags:

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?

like image 988
Shrey Gupta Avatar asked Jul 24 '13 17:07

Shrey Gupta


People also ask

Why do jQuery class manipulation functions not work with SVG elements?

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, " " ) : " " );

How do I change the class of an SVG file in jQuery?

While jQuery is a HTML library, we agreed that class support for SVG elements could be useful. Users will now be able to call the. addClass (), . removeClass (), . toggleClass (), and. hasClass () methods on SVG. jQuery now changes the class attribute rather than the className property.

What is hasclass method in jQuery?

jQuery hasClass () Method Last Updated: June 4, 2021 In jQuery, if you want to find out whether some elements (or a single element) are assigned a particular CSS Class then use the.hasClass () method. It returns true when it finds the CSS class else returns false.

Is there a jQuery SVG plugin for HTML elements?

// Instead of .addClass ("newclass") $ ("#item").attr ("class", "oldclass newclass"); // Instead of .removeClass ("newclass") $ ("#item").attr ("class", "oldclass"); Show activity on this post. There is element.classList in the DOM API that works for both HTML and SVG elements. No need for jQuery SVG plugin or even jQuery.


2 Answers

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.

like image 177
Louis Ricci Avatar answered Oct 25 '22 15:10

Louis Ricci


As Bergi pointed out in comments, jQuery silently fails on SVG elements on account of className returning an SVGAnimatedString object instead of a normal DOMString.

See this JSFiddle for a comparison.

I was tempted to submit a pull request on this, but did a quick project search, and apparently the jQuery project stance on SVG issues is wontfix: https://github.com/jquery/jquery/pull/1511

If you're using D3, you could use d3.select(this).classed('node'). Note that D3 correctly returns for both HTML elements and SVG elements.

like image 36
Ben Mosher Avatar answered Oct 25 '22 14:10

Ben Mosher