How to create html id attributes dynamically?

0👍

I’m not totally sure what you are using to render your HTML since your code doesn’t look like pure HTML to me. However, the answer stays the same, you can use the index of the loop as a part of your id to make it unique. Something like this:

<canvas id="pie-char-@id></canvas>

with @id is your index of the loop.

If your current renderer doesn’t support that, you may want to add a counter outside of the loop, use that counter as part of your id. Then increase the counter at the end of the loop.

👍:-1

See Does ID have to be unique in the whole page?

The HTML Id needs to be unique in your whole HTML-Instance. Use “class” instead of “id”.

Id is and was never designed to be used multiple times. Id is designed to be a unique identifier.

Leave a comment