Skip to content
Advertisement

Set unique ID to SVG graph path

How to set unique ID to SVG graph path elements. The graph is created using leaflet.js Path elements are dynamically generated and I need to style them individually, so I thought of giving them unique ID.

JavaScript

Please help.

Advertisement

Answer

As I’ve commented you don’t need an id for styling. You can use nth-of-type or nth-child.

JavaScript

Also you can reduce verbosity by moving stroke="#3388ff" stroke-opacity="1" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" fill="#3388ff" fill-opacity="0.2" fill-rule="evenodd" in CSS

JavaScript
JavaScript

However if you still think you need to add `id-s in JavaScript, here is how I would do it (btw @DogukanCavus is perfectly valid):

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement