It started with a comment on the RGB solution, but I could not fit it, so I converted it to a response. The source of inspiration for which are exclusively RGB.
The RGB solution worked for me. However, I would like to point out a couple of points that may help others coming to this post (like me) who are not so familiar with which SVG, and who could very well generate their SVG file from a graphics package (like me did it).
Therefore, to apply RGB solutions, I used:
CSS
<style> rect.btn { stroke:#fff; fill:#fff; fill-opacity:0; stroke-opacity:0; } </style>
Jquery script
<script type="text/javascript" src="../_public/_jquery/jquery-1.7.1.js"></script> <script type="text/javascript"> $("document").ready(function(){ $(".btn").bind("click", function(event){alert("clicked svg")}); }); </script>
HTML code to encode the inclusion of your pre-existing SVG file in the group tag inside the SVG code.
<div> <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <g> <image x="0" y="0" width="10" height="10" xlink:href="../_public/_icons/booked.svg" width="10px"/> <rect class="btn" x="0" y="0" width="10" height="10"/> </g> </svg> </div>
However, in my case, I have several SVG icons that I want to click on, and including each of them in the SVG tag has become cumbersome.
Therefore, as an alternative approach where I could use classes, I used jquery.svg. This is probably a shameful application of this plugin that can do all kinds of things with SVG. But it worked using the following code:
<script type="text/javascript" src="../_public/_jquery/jquery-1.7.1.js"></script> <script type="text/javascript" src="jquery.svg.min.js"></script> <script type="text/javascript"> $("document").ready(function(){ $(".svgload").bind("click", function(event){alert("clicked svg")}); for (var i=0; i < 99; i++) { $(".svgload:eq(" + i + ")").svg({ onLoad: function(){ var svg = $(".svgload:eq(" + i + ")").svg('get'); svg.load("../_public/_icons/booked.svg", {addTo: true, changeSize: false}); }, settings: {}} ); } }); </script>
where is the HTML
<div class="svgload" style="width: 10px; height: 10px;"></div>
The advantage of my thinking is that I can use the appropriate class where icons are ever needed and avoid a lot of code in the HTML body, which contributes to readability. And I only need to include the previously existing SVG file once.
Editing: Here is a more accurate version of the script provided by Keith Wood: using the .svg download URL setting.
<script type="text/javascript" src="../_public/_jquery/jquery-1.7.1.js"></script> <script type="text/javascript" src="jquery.svg.min.js"></script> <script type="text/javascript"> $("document").ready(function(){ $('.svgload').on('click', function() { alert('clicked svg new'); }).svg({loadURL: '../_public/_icons/booked.svg'}); }); </script>