1

I have an HTML file that looks somehow like this:

<html>
 <div id="test"> ... </div>
 <object data="pic.svg" [...]></object>
</html>

Inside pic.svg, I have an element, let's say, a circle, and I want to realize something like that:

<circle onClick="doSomething()" [...]>

Now, in the js function doSomething() (i.e. when someone clicks on the circle) I want to change my "test"-div. How to do this?

2 Answers 2

0

You can access the parent document (and consequently the elements there) from inside the svg like this:

var divInParentDocument = window.parent.document.getElementById('test');

Here's a (slightly more complex) example showing how to call a function in the parent html document from inside an svg.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want the SVG to live in the same DOM as the HTML, best to use an embedded <svg> element. It's pretty well supported.

Something like

<svg id="svgroot" width="600" height="600"
     version="1.1" xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">
<script type="text/ecmascript">//<![CDATA[
   function doSomething() {
       document.getElementById('test').appendChild(...);
   }
//]]></script>

<circle onClick="doSomething()" />
</svg>

2 Comments

Unfortunately, my svg file is quite large, so I'd like to avoid having the svg code in the HTML file...
@vauge that's what xlink is for. You can just link in an external SVG.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.