Skip to content

D3 .classed() not updating stroke/fill in SVG elements, however inline styles works. #2305

Closed Answered by dheerajy1
dheerajy1 asked this question in Q&A

You must be logged in to vote

hello,

I finally managed to resolve the issue.

Reason:

The issue was caused by using a randomly generated ${id} in the style selector. Since the direct parents of the targetElements did not have this generated ID, the target elements did not apply the styles, even though .is-target was added with .classed(true).

Solution:

For the styles to take effect, the parent element must have the corresponding class. Instead of using a randomly generated ${id}, I used the existing class of the element. This ensures that the target elements correctly inherit and apply the styles.

Working code

  // Select the existing <style> element inside the SVG
    let styleElement = d3.select(svg).select("style");

Replies: 1 comment

You must be logged in to vote
0 replies
Answer selected by dheerajy1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant