svg, color group child elements
NickName:BrinkDaDrink Ask DateTime:2015-06-11T05:07:18

svg, color group child elements

I created a SVG out of illustrator. The elements have the fill on the paths. I am trying to change the color based on the group id that the path is in.

I would like to change all child elements fill colors in the same group to the same color when an event happens.

Here is a jsFiddle: http://jsfiddle.net/cdcgbu6g/6/

function ChangeColor(zone,color,colorCode) {
document.getElementById(zone).children().css( "fill", colorCode );
};

The javascript would be in an external .js file.

When you click a color square, the fill value on the two objects should change that are under the group id of "Color_1".

Everything I read says this should work.

Copyright Notice:Content Author:「BrinkDaDrink」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30767314/svg-color-group-child-elements

Answers
Kaiido 2015-06-15T23:52:56

As stated by @Paul LeBeau your first snippet did work well, just a jsFiddle configuration issue. \n\nHowever, while revising your post, you brought a new error : \n\nfunction ChangeColor(zone,color,colorCode) {\n document.getElementById(zone).children().css( \"fill\", colorCode );\n};\n\n\nThis code won't work because you are trying to call jQuery methods (.children() and .css()) on a non jQuery object (document.getElementById() does return a DOM object ). \n\nYou could rewrite this function to :\n\nfunction ChangeColor(zone,color,colorCode) {\n $('#'+zone).children().css( \"fill\", colorCode );\n};\n\n\nNow we're calling a jQuery object and everything works well, just like in your first snippet where you weren't using jQuery.",


More about “svg, color group child elements” related questions

svg, color group child elements

I created a SVG out of illustrator. The elements have the fill on the paths. I am trying to change the color based on the group id that the path is in. I would like to change all child elements ...

Show Detail

SVG group to child transforms

I have a group that has 2 or more SVG elements. Group has a SVG matrix applied and the same apply for children. Each element, including group can have any transformations applied through the matrix (

Show Detail

How to use snap to fill color for the whole group of elements in SVG

I want to fill color for the whole group of elements for a SVG image. I've grouped my elements together with the tag like this <g id="calendar"> <path d="M265.8,631.3l-26.7,8c1.7,5.4,3.5,...

Show Detail

SVG Fill Width to Child Elements

I'd like my container SVG element to scale in order to fit its child group elements or for it to present scrollbars on overflow. I was wondering if there was a css property in order to do so. E.g....

Show Detail

SVG: border outline for group of elements

I'd like to add a border to a number of elements which are grouped by <g>. As an example: <g id="group"> <circle cx="125" cy="125" r="65" fill="orange"

Show Detail

How to make a random color function acting on svg groups call the same color for all elements in group

I have a function that creates a random color. I use this function in another function to call the color onclick of an svg groups. The problem is that all elements in the group get a random color, ...

Show Detail

How to prevent child component SVG fill color in parent SVG in angular?

I have added D3 map as a child component to the parent. But when ever I am changing map background color using .attr('fill', '#00A3E0'), parent component's SVG elements get the same color. I have ...

Show Detail

Best way to move a group of SVG elements

I'm looking for the best way (the faster, the cleaner...) to move a group of SVG elements. I have three ways in mind : do a loop on all elements and, for each of us, change the x and y attributes ...

Show Detail

Hover effect on SVG group elements

Please excuse my ignorance, SVG is very new to me. I'm trying to get a hover effect on a group of elements in my inline SVG. Each group of elements is a closely positioned set of circles. I can ach...

Show Detail

SVG child elements that affect it's parent

Many of elements' properties in SVG are usually declared as attributes or properties inside the element. F.ex width and height: <svg width="500" height="300" xmlns="http://www.w3.org/2000/svg"&...

Show Detail