How to add an ID to dynamically created div with JavaScript?

Sometimes, we want to add an ID to dynamically created div with JavaScript.

In this article, we’ll look at how to add an ID to dynamically created div with JavaScript.

How to add an ID to dynamically created div with JavaScript?

To add an ID to dynamically created div with JavaScript, we set the id property.

For instance, we write

cartDiv.id = "someID";

to set the cartDiv div’s ID by setting its id property to the ID name string we want.

Conclusion

To add an ID to dynamically created div with JavaScript, we set the id property.