In this JavaScript post, I demonstrated how to create HTML elements using JavaScript.
But many times, we need to create elements with class and id attributes so that we can target those newly created elements.
We can use setAttribute() Method to set id and class attributes.
setAttribute() Method
This method adds the defined attribute to an HTML element, and also adds a defined value to it.
For e.g. we can set background-color of an element like this:
el.setAttribute("style", "background-color: green;");
createElement with id
You will be able to see it using inspect element in any browser.

createElement with class
The same way we can set the class. Just like this:
You will be able to see it using inspect element in any browser.

attributes class create elements html tags id