How to click on a part of an SVG within a website with Javascript?

svg onclick javascript
svg click event not working
react svg onclick
svg path onclick
click svg element javascript
trigger click on svg element
javascript load svg from file
svg click is not a function

Javascript provides a method called click().

This works for example:

<a id="a" href="index.htm">Click here</a>



But what about SVG-Elements?

  <g id="a" .....></g>

This won't work:


I can successfully select the g-Tag, but it has no click method:

document.getElementById(...).click is not a function

Is it somehow possible to click an element within an SVG-Tag with Javascript?

You can just create and dispatch the event manually e.g.

document.getElementById("a").dispatchEvent(new Event('click'));

This is all the click() method does underneath the hood. It's just a convenience method.

This will work with any SVG element, including <g> elements.

Managing SVG Interaction With The Pointer Events Property , If you put your pointer in the right place (the shaded path) then you If you tried to click on some white space, you might be really confused instead. a practical guide to designing and building forms for the web. With it, we can manage which parts of an SVG document or element can HTML; CSS; JS. I'm messing around with SVG and I was hoping I could create SVG files in Illustrator and access elements with Javascript. Here's the SVG file Illustrator kicks out (It also seems to add a load of junk to the beginning of the file that I've removed)

use dispatchEvent(new Event('click')) if .click() is not working. Please check the fiddle at

Using Javascript with SVG, Inside the SVG itself; External to the SVG (within the HTML document or as a separate separation of concerns, and easily reuse the JS for multiple SVGs on a website. Also, if you right click on the external SVG you should have an the JS code part of XML, and if you use < or >, it will break (as in this  The HTML <svg> Element. The HTML <svg> element is a container for SVG graphics.. SVG has several methods for drawing paths, boxes, circles, text, and graphic images.

Not entirely sure whether JavaScript has an actual click() event (I do know it can be inserted as: onclick() in HTML). jQuery has a click() event. That said, I believe you have to add an eventlistener if you are using JavaScript and no jQuery.

document.getElementById("a").addEventListener('click', function() {
     //do something

onclick, The onclick attribute specifies some script to run when the element is clicked. Technologies Overview · HTML · CSS · JavaScript · Graphics · HTTP · APIs / DOM · Browser Extensions · MathML Jump to section onclick="alert('You have clicked the circle.')" /> </svg>. Open in CodePen. Open in JSFiddle  Thanks to being defined in XML, SVG images are much more flexible than JPG or PNG images, and** we can use CSS and JavaScript to interact with them**. SVG images can even contain CSS and JavaScript. SVG images can render vector-style images a lot smaller than other formats, and are mainly used on logos and illustrations. Another huge use case

Have you tried the "onclick" directly on the HTML?

I know it's brutal, but did it works?

  <g id="a" onclick="your code here"  .....></g>

Links with Inline SVG, Staying on Target with Events, It's pretty common to use SVG within an anchor link or otherwise "click/tappable thing" on a web page. It's also increasingly common that the  Preparing an SVG for use on the web is a simple process and no more complicated that exporting a JPEG or PNG.Work as you typically would in your preferred vector graphics editor (Illustrator, Sketch, Inkscape [free], etc [or even Photoshop if you use shape layers]) with the graphic at the size that you expect to use it.

Click SVG Element to Focus (and Style), A reader writes in: Is there anything on your site that shows me how to make an SVG As in, I have an SVG and I click on part of it and it should reveal an outline on that Typically, when I think “clickable”, I think JavaScript. How to Scale SVG to Fit within a Certain Size (without distorting the image) Probably the most common requirement is to have an SVG icon scale to fit a specific size, without distortion. The viewBox attribute is really all you need here, although you can use preserveAspectRatio to adjust the alignment.

How to Translate from DOM to SVG Coordinates and Back Again , If you position this SVG in an 800×600 pixel area, each SVG unit maps JavaScript to increase the radius by ten units when a circle is clicked:. After that, we need to add javascript code to the SVG. Script code goes in the <script> tag. We open the svg file in a text editor and add: <script xlink:href="myscript.js" /> and we also need to add onload="on_load(evt);" to the <svg> tag. This means that after the browser loads, it will call a javascript function called on_load.

How To Display Additional Information By Clicking Image , I suggest opening that section in a separate window or tab for better Setting interactivity to the SVG image Specifying javascript functions on events in SVG. Used in SVG element cross-references, link targets, the JavaScript getElementById() methods, CSS selectors, ARIA relationship attributes, and more. Some SVG elements will have no effect without an id. Value must be a non-empty string that does not contain any whitespace. The ID should be completely unique within a document.

  • Thanks. This works on a random website I tried ( But somehow it does not work for the website I need it for.. No error. If I run it in the browser, it returns true but does not execute the click.
  • Hmm... It says to click shapes in the g-element.. They are not containers... In my case, the g-element contains a text tag, a rect tag, a polygon tag... But none of these work: document.querySelector("#a>rect").click();, document.querySelector("#a>text").click();... The click()-Method does not seem to exist for those too...
  • I use the click() for a Selenium UI test workaround for Safari.. Adding an event listener works (does not fail), but clicking it then fails with the same exception for me..
  • As mentioned above, and maybe an edit to my answer would do the trick: <g> itself cannot be clicked.
  • Hi, I want the Javascript code to click the element. I don't want to define what happens when it is clicked.