Potatoswatter
Potatoswatter

Reputation: 137940

jQuery with an SVG document

jQuery is designed to be used in HTML pages with JavaScript.

SVG uses the same DOM Level 2 as HTML but is XML-based and specifies ECMAScript.

What problems could result from using jQuery with SVG? Would it be preferable to embed such SVG as an element in an HTML document?

Note, I'm not referring to the "jQuery SVG" drawing library which provides for manipulation of an SVG element in an HTML document as a graphics port using jQuery syntax. What I want is to use jQuery selectors and event management on SVG elements, with the SVG root maybe being inside HTML or maybe not.

Upvotes: 6

Views: 352

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137940

Trying it out, selectors seem to work but other things don't. The $( function() { } ) idiom to initialize the page doesn't work because SVG only passes an SVGLoad event to the top <svg> element. $('svg').bind('SVGLoad', function(){}) does work, though.

Dynamically adding elements with .append puts them in the DOM such that they don't get rendered, at least on Firefox. The phantom elements remain invisible even after the document is re-rendered including an element dynamically added without jQuery. $().attr(key, value) may change an attribute but not update the onscreen display.

It all seems unfortunately broken. More features work once it's embedded in an XHTML document. But defects such as the above remain and it's probably better to use another framework. Maybe give jQuery SVG another look…

Upvotes: 2

Related Questions