When I use the onclick
attribute of an object
-element in an html document, it does not respond to clicks.
In my document I have an svg image and store that in an object
-element, since there is some animation in the image that would get lost by just using an img
-tag.
In the simplified example below, the onmouseover
works on both objects, but the onclick
just works on the object
without the svg-image.
document.getElementById('test1').onmouseover = hover;
document.getElementById('test1').onclick = click;
document.getElementById('test2').onmouseover = hover;
document.getElementById('test2').onclick = click;
function hover() { alert('Hovered');};
function click() { alert('Clicked');};
<object id='test1' data="https://upload.wikimedia.org/wikipedia/commons/0/01/SVG_Circle.svg" height="50px"></object>
<object id='test2' height="50px" border="1px solid black">some object</object>
Is there something I am doing wrong here? Or is there an alternative that does work?
The answers given for this (and related questions) advise to use pointer-events: none
on the svg-image and wrap it in a div and apply the listeners to that div. But I need the svg-image to respond to mouse events
, and therefore cannot set pointier-events: none
.