Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *