I am trying to develop an interactive SVG map and I want to do stuff when the mouse enters rectangles within the SVG image. Currently, the code logs to the console when my mouse enters the SVG image, but not when I mouse over the rectangles. Any help would be much appreciated. Thanks!
<object onload="svgOnLoad()" id="activeSVG" data="SVGNAME.svg" type="image/svg+xml">
</object>
<script>
function svgOnLoad() {
console.log("SVG Loaded");
$("#activeSVG").mouseenter(function(e) {
console.log("In the SVG")
});
//Executed when the mouse enters an SVG rect element
$("rect").mouseenter(function(e) {
console.log("Mouse Entered rectangles!!")
});
}
</script>
After messing around with it for a bit I found what needed to be added. You need to get the content of the svg object and then play with it from there. I've included the new script code below.
<script>
function svgOnLoad() {
// Get SVG object
var officeMapFloor = document.getElementById("activeSVG");
// Get the content of the SVG object
var svgDoc = officeMapFloor.contentDocument;
// Access an ID within the content of the object
var rect = svgDoc.getElementById("siesta");
// Apply the event listener
rect.onmouseover = function(){
console.log("Finally in the rectangle");
};
}
</script>