How can i create unique id for html elements | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 3

How can i create unique id for html elements

I am using createelement in javascript but is there any way to make the id of the element new everytime i create the element. For example, the first time i create the id will be "test1" then when i run the code again it will return an element with the id "test2" Is it possible????

3rd Jun 2020, 10:20 AM
Asef Dian🇧🇩
Asef Dian🇧🇩 - avatar
1 Réponse
+ 7
Asef Dian🇧🇩 Hey. Of course you can do that. Here's the solution : <script src="https://cdnjs.cloudflare.com/ajax/libs/node-uuid/1.4.8/uuid.js"></script> Include this in your head tag. Now you can make a unique id each time you run the code. Like so : const id = uuid(); Now use this 'id' as your element's id. That's all :))
3rd Jun 2020, 10:27 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar