Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Position of svg element using JS

Trying to make an svg rectangle that moves when button pushed. Right now I just want the x to be modified by a function.

function modX()
{
  document.getElementById("rectangle").transform = 'translate(295 115)';
}
var x = 20;
var y = 20;
modX();
<svg  width="1000" height="1000" >
  <rect id="rectangle" x="0" y="20" width="100" height="100" 
  style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)"></rect>
</svg>

I'm fairly new to code so please avoid css or jquery.

like image 540
Captainninjakid Wright Avatar asked Mar 23 '18 18:03

Captainninjakid Wright


1 Answers

you can change its x by using javaScript document.getElementById("rectangle").setAttribute('x', X_value) set the X_value to the value you want it to change.

like image 195
Erez Sharabi Avatar answered Oct 14 '22 05:10

Erez Sharabi