Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Svg click events not working properly

I am trying to make my SVGs look like a "pie shape" which appears to be all fine, besides, i want each of them to have a different click event.

function one() {
    alert("1");
}
function two() {
    alert("2");
}
function three() {
    alert("3");
}
function four() {
    alert("4");
}
<svg style="position: absolute;height:auto;width:auto;" onClick="one()">
<path d="m 25.857864,25.857865 a 20,20 0 0 1 28.284271,-1e-6 L 40,40 Z"/>
</svg>
<svg style="position: absolute;height:auto;width:auto;" onClick="two()">
<path
   d="m 25.857864,-54.142135 a 20,20 0 0 1 28.284271,-10e-7 L 40,-40 Z" 
   transform="rotate(90)" />
</svg>
<svg style="position: absolute;height:auto;width:auto;" onClick="three()">
<path
   d="m -54.142136,-54.142135 a 20,20 0 0 1 28.284271,-10e-7 L -40,-40 Z" 
   transform="scale(-1)" />
</svg>
<svg style="position: absolute;height:auto;width:auto;" onClick="four()">
<path 
   d="m 25.857864,25.857865 a 20,20 0 0 1 28.284271,-1e-6 L 40,40 Z" 
   transform="matrix(0,1,1,0,0,0)"/>
</svg>

However my problem is, whenever i try, the last SVG in the code seems to be covering other SVGs in the code, such that only the last function {four() in the example} would be called out no matter which part of the circle i click on

like image 378
EnRico Lam Avatar asked Oct 17 '22 07:10

EnRico Lam


1 Answers

One svg tag and assign onClick function to path tag like this and it works fine:

function one() {
    alert("1");
}
function two() {
    alert("2");
}
function three() {
    alert("3");
}
function four() {
    alert("4");
}
<svg style="position: absolute;height:auto;width:auto;">
<path d="m 25.857864,25.857865 a 20,20 0 0 1 28.284271,-1e-6 L 40,40 Z" onClick="one()"/>
<path
   d="m 25.857864,-54.142135 a 20,20 0 0 1 28.284271,-10e-7 L 40,-40 Z" 
   transform="rotate(90)" onClick="two()"/>
<path
   d="m -54.142136,-54.142135 a 20,20 0 0 1 28.284271,-10e-7 L -40,-40 Z" 
   transform="scale(-1)" onClick="three()" />

<path 
   d="m 25.857864,25.857865 a 20,20 0 0 1 28.284271,-1e-6 L 40,40 Z" 
   transform="matrix(0,1,1,0,0,0)" onClick="four()"/>
</svg>
like image 163
Sagun Gautam Avatar answered Oct 20 '22 22:10

Sagun Gautam