Add onClick event to a group element svg element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:SVG

Description

Add onClick event to a group element svg element

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from w  ww  .  j  a  va2 s .  c om
document.querySelector("g").addEventListener("click", () => console.log("clicked"))
    }

      </script> 
   </head> 
   <body> 
      <svg width="300" height="300"> 
         <g> 
            <rect x="0" y="0" width="100" height="100"></rect> 
            <rect x="200" y="200" width="100" height="100"></rect> 
         </g> 
      </svg>  
   </body>
</html>

Related Tutorials