0

How can i attach a Onclick event handler to all SVG path element? I have loaded my SVG in an iframe. I tried to use the following but the click event is not firing.

jQuery(function ($) {
        debugger;
        $('path').click(function () {                
            alert("Hello");
        });
}); 
<iframe id="frame" src="myFile.svg" border="0" width="100%" height="450px"></iframe>
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
Anish
  • 872
  • 5
  • 21
  • 42

1 Answers1

2

Try:

$("#frame").contents().find("path").on("click", function () {
    alert("Hello");
});

Events from within an iframe do not bubble through it to the containing document. So, if even possible to bind events this way, you need to find the path elements inside the iframe and bind click events to them.

Ian
  • 50,146
  • 13
  • 101
  • 111