Search code examples
javascriptcsssvgheightwidth

Change height and width of SVG object using JavaScript


I want to change height and width of an svg object on a button click. I tried it but it doesn't work:

function modify() {
    document.getElementById('circle1').style.height = "10px";
    document.getElementById('circle1').style.width = "10px";        
}

Solution

  • In SVG width and height of <rect> elements are attributes and not CSS properties so you'd need to write

    document.getElementById('cercle1').setAttribute("height", "10px");
    

    similarly for the width.