How to manipulate SVG element style with javascript?


Tags: javascript,html,css,svg

Problem :

I've created an inline svg with a few rectangle elements and set the same color to all of them in css. I gave an id to each of them and now want to manipulate them with plain javascript in order to change the color of each rectangle. My javascript code does validate correct and I can see in the console that the rectangles are selected, but their color didn't change. Any suggestion on what I'm doing wrong and how I can fix it? Thanks in advance! Example of my html code:

<rect id="rect1" width="40" height="230" x="20" y="170" rx="10"/>
<rect id="rect2" width="40" height="300" x="60" y="100" rx="10"/>

My javascript code:

var rect1 = document.getElementById("rect1");
console.log(rect1);
rect1.setAttribute("style", "color: red");


Solution :

SVG elements use the fill property to color the background of the element, not color.

rect1.setAttribute("style", "fill: red");

Read more about styling SVG elements here.
https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Fills_and_Strokes


    CSS Howto..

    How can I resize images that are loaded onto a page?

    How to show the vertical image into horizontal manner using HTML and CSS?

    How can i spin an image -360 degrees and +360 degrees using the -webkit and jQuery?

    How to automaticly let HTML cut the edges based on the setted height of the image?

    How to display a centered background of fixed width

    How to add class with data-attribute?

    How do I animate this box to have a pop out effect?

    how do I prevent max-height transition from scrolling the page to the top?

    Styling Tables - How to use column groups to modify TH tag located in that column

    I want all columns() in a
    have the equal length, how to achieve this?

    bootstrap css how to resolve conflict

    How to position text over an image in css

    How can I make an image continue across multiple sections with breaks in between?

    How to add slide animation when showing elements

    How to keep image position despite window/device size?

    How to hide only a part of the content of a li with CSS?

    How to vertically center div in floated div?

    How to display a box of color beside a table row using CSS?

    How to prevent this css shape to scroll left and right

    How should I organize the contents of my CSS file(s)?

    How to delay the display of the background image in a div

    how can i add a separator gif between two content areas something like this?

    How to disable the Dropdown?

    CSS how to vertically center image and text without changing style of image

    How can I remove the generic HTML/CSS tooltip? [duplicate]

    How to make an iFrame window scrolls with overflow:hidden?

    How do I edit the code of a WordPress site?

    How to Position an Image underneath a form in CSS

    How to make a scrolling row of Divs using CSS & Javascript?

    How can one create horizontal tree view using CSS and HTML?