I have a situation where I load an SVG graphic from an external file and embed it into the html with the object
tag method, and then I want to make the SVG interactive so that when some elements in the SVG are clicked, some other elements within the html page are affected. I plan to use this so that when some element of the graphic is clicked, more detailed information about the topic is shown in another element.
Therefore, the main question is, which method do I need use to embed the SVG and how do I need to link external Javascript files so that the Javascript functions in the same way as all the other external JS files included in the html page at the end of body
, and I can target all elements inside and outside the SVG. A big plus would be if jQuery could also be enabled, which I understand is also a bit of an issue, since the DOMs differ.
I do not necessarily need to support IE8 or lower.
For example, if I have the following structure:
<body>
<!-- SVG graphic -->
<object type="image/svg+xml" data="images/somegraphic.svg">
Your browser does not support SVG
</object>
<!-- details appear in this div -->
<div class="details"></div>
<!-- external JS file -->
<script scr="somejavascript.js">
</body>
And I would want to do e.g. this in the external JS file. I.e. I would need the external JS to be able to target both elements inside the SVG and outside it in the html.
// recognize click inside the SVG, assume an element inside the SVG has a class "clickable"
$(".clickable").click(function(){
$(".details").text("Detailed information about what you just clicked.");
});