views:

225

answers:

1
+1  Q: 

jQuery under SVG

Is there any way I can run jQuery under SVG instead of HTML?

I know about jQuery svg plugin but this is not what I want.

As for now the jQuery engine contains some statements like this:

div = document.createElement("div");
div.style.display = "none"; 

It fails with exception during jQuery initialization under SVG because SVG document is unable to create div element. And there are some other HTML-specific places in jQuery which fail in unpredictable places under SVG.

Is there any way to resolve this problem?

+1  A: 

I hacked an old version of jquery around a bit, and it is possible to run it in svg-only mode with some modifications. Feel free to take the changes and do whatever you want with them. Here's a demo and here's the modified jquery sourcecode.

It's incomplete and probably has some issues if you want to use it in both HTML and SVG, but it's a start, and works to some extent at least.

Erik Dahlström
Great! That's exactly what I'm looking for, and it is working. Thanks.
Volodymyr Frolov
There was one more issue with classes of elements. Class selectors and methods didn't work because in HTML elem.className is a string but in SVG it is an object. String class is taken from elem.className.baseVal. I have patched newest (1.4.2) version of jQuery. One more issue is that SVG doesn't call initialization methods so I introduce jQueryInitialize and call it in onload. Here is a link: http://hgcoaxial.appspot.com/static/jquery-1.4.2.svg.js to the patched jQuery. And here is more sophisticated example: http://hgcoaxial.appspot.com/static/jquery-1.4.2.svg.js of jQuery under SVG.
Volodymyr Frolov
Thanks, almost works, only - how do I animate e.g. position of a circle? jQuery works with CSS attribs, and circle's `cx` / `cy` has no CSS counterpart IIUC. I've tried `top` but no effect (although does not throw).
Ondra Žižka