I want to create an interactive SVG HTML-Page. For example: If I click on a rectangle, I want to display a new SVG-Image.
Because my SVG-Files are separate, I want to include them via the HTML-Object Tag.
I've browsed the web for a while, but I just fail to adress single elements of my SVG-File to change their attributes or add functions etc.
My HTML-File:
<!DOCTYPE html>
<html>
<body>
<object data="svg-test.svg" type="image/svg+xml" id="svg" width="100%" height="100%"></object>
<script>
window.onload=function() {
// Get the Object by ID
var svgObject = document.getElementById("svg");
// Get the SVG document inside the Object tag
var svgDoc = svgObject.contentDocument;
// Get one of the SVG items by ID;
var svgItem = svgDoc.getElementById("svgID");
// Set the colour to something else
svgItem.setAttribute("fill", "lime");
};
</script>
</body>
</html>
The SVG-File:
<svg width="900" height="600" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="100" y="120" width="200" height="350"
style="fill:white;stroke:black;stroke-width:2;fill-opacity:1.0;stroke-opacity:1.0"
id="svgID"/>
</svg>
Instead of using contentDocument I also tried getSVGDocument() but this also did not work.