Hi I have the following code and I want to be able to change the radius of a circle by pressing a button I dont know what to use after style. in the
document.getElementById(“circle1″).style.r=”10”; part of the code
<html>
<head>
<script>
function circle() {
document.getElementById("circle").style.r="50";
}
function circle1() {
document.getElementById("circle1").style.r="10";
}
</script>
</head>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="svg" >
<circle id = "circle" cx = "100" cy = "800" r = "30" stroke="blue" stroke-width = "2" fill = "black"/>
<circle id = "circle1" cx = "20" cy = "500" r = "30" stroke="blue" stroke-width = "2" fill = "black"/>
</svg>
<body>
Set size of circles
<input type="button" onclick="circle()" value="big" />
<input type="button" onclick="circle1()" value="small" />
</body>
</html>
As noted by pp19dd, in his answer, the key is
setAttribute(), but as it seems you want to increase/decrease therattribute of thecircleelements (and not simply set it to a particular value), you’ll need to usegetAttribute()as well.This is a fairly simple function and implementation that, I think, does what you wanted:
JS Fiddle demo.
Note that I’ve not implemented any checks for invalid negative values for the circle’s
rattribute. You may want to add that yourself.And I used
document.querySelectorAll()for simplicity (rather than two explicit calls todocument.getElementById()). This will cause problems in Internet Explorer, though I’m unsure as to how well implemented SVG is in Internet Explorer, so it might not make things any worse.Having said all that, though, it seems that IE 9 implements the demo perfectly. Which surprises me no end..! IE 8, and lower, though, I’m unable to say.
References:
document.querySelectorAll().element.getAttribute().element.setAttribute().parseInt().