I am trying to earn some JavaScript by manipulating SVG docs. I have the following code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" height="24" width="400" id="tmcBarLoading">
<rect id="outerrect" width="400" height="24" x="0" y="0" style="stroke-width:2px;stroke:grey;fill:none;"/>
<rect id="innerrect" x="2" y ="2" width="0" height="20" style="stroke-width:0px;" fill="blue">
<animate attributeName="width" attributeType="XML" begin="0s" dur="2s" fill="freeze" from="0" to="396"/>
</rect>
</svg>
And trying to set up a simple alert when an element inside the SVG doc is clicked:
$(document).ready(function () {
'use strict';
var img = document.getElementById("spikeroad");
console.log(img);
var delta = img.getElementById("outerrect");
delta.addEventListener("click", function() {
alert('Delta clicked');
}, false);
});
The script can find the img well enough but then fails to attach the alert to he image when it is clicked. I hope to be able to dynamically attached animation events to objects using JavaScript but I need to work out how to identify them first.
this is all sitting in this html:
<html>
<head>
<title>My Title</title>
<meta charset="utf-8">
<script defer="defer" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script defer="defer" src="controls.js" type="text/javascript"></script>
</head>
<body>
<img id="spikeroad" src="map.svg" alt="loadbarspike"/>
</body>
</html>