Search code examples
javascriptjquerysvgfill

Change the fill of all elements inside an svg using javascript


I would like to change the fill color of all elements inside an SVG. The elements are both type rect and polygon. I have not set a default color.

Here is a sample file:

http://www.kingoslo.com/instruments/test.svg

How can I do this most elegantly?


Solution

  • If you have jQuery:

    $('svg').children().css('fill', '#ffffff');
    

    EDIT: Note that this assumes the rects and polygons are the direct children of the svg element.