You should use the .setAttribute() method:

g = document.createElement('div');
g.setAttribute("id", "Div1");
Answer from lkaradashkov on Stack Overflow
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-create-element-with-class
Create an Element with Classes or ID in JavaScript | bobbyhadz
Copied!<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> <style> .bg-yellow { background-color: yellow; } .text-lg { font-size: 2rem; } </style> </head> <body> <div id="box"></div> <script src="index.js"></script> </body> </html> ... And here is the related JavaScript code. ... Copied!// โœ… Create element const el = document.createElement('div'); // โœ… Add classes to element el.classList.add('bg-yellow', 'text-lg'); // โœ… Set ID attribute on the element el.setAttribute('id', 'my-id'); // โœ… Add text content to the element el.textContent = 'Hello world'; // โœ… Or set the innerHTML of the element // el.innerHTML = `<span>One, two, three</span>`; // โœ… add element to DOM const box = document.getElementById('box'); box.appendChild(el);
๐ŸŒ
Quora
quora.com โ€บ Can-you-create-an-element-in-JS-with-an-ID-or-a-class
Can you create an element in JS with an ID or a class? - Quora
Answer (1 of 4): There are times you might want to dynamically generate elements on a page and add classes to style them. You can create elements with ids and/or classes with plain Javascript with the following commands: [code]var newElement = document.createElement(โ€œdivโ€); // to add a ...
Discussions

javascript - Create Element with id or class with React Dom - Stack Overflow
Stack Internal Implement a knowledge platform layer to power your enterprise and AI tools. Stack Data Licensing Get access to top-class technical expertise with trusted & attributed content. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to create html element using JavaScript and create class/id to it - Stack Overflow
I want to create html element using JavaScript and add class or id, so I can edit this element later. I know how to create element: More on stackoverflow.com
๐ŸŒ stackoverflow.com
html - Add ID/class to objects from createElement method - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
๐ŸŒ stackoverflow.com
html - Javascript: How do I create element with id class and id which is some variable? - Stack Overflow
Javascript: How do I create element with id class and id which is some variable? Where I am not giving ID as some value but ID is equal to some variable More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ javascript dom โ€บ javascript createelement() method
JavaScript document.createElement() By Practical Examples
January 18, 2020 - If you want to add an id to a div, you set the id attribute of the element to a value, like this: let div = document.createElement('div'); div.id = 'content'; div.innerHTML = '<p>CreateElement example</p>'; document.body.appendChild(div);Copy ...
๐ŸŒ
HTML Dog
htmldog.com โ€บ guides โ€บ javascript โ€บ advanced โ€บ creatingelements
Creating Elements | HTML Dog
Creating elements using HTML tags isnโ€™t the only way to do it โ€” in fact itโ€™s possible to create, modify and insert elements from JavaScript. Hereโ€™s an example that creates a div, adds some text to it and appends it as the last element in the body: var div = document.createElement('div'); div.textContent = "Sup, y'all?"; div.setAttribute('class', 'note'); document.body.appendChild(div); So document.createElement is used with an HTML tag to create the element.
Find elsewhere
๐ŸŒ
Tutorial Reference
tutorialreference.com โ€บ javascript โ€บ examples โ€บ faq โ€บ javascript-how-to-create-an-element-with-id-and-classes
How to Create an Element with an ID and Classes in JavaScript | Tutorial Reference
For modern development, you should ... fundamental part of dynamic web development. Follow the three-step process: create (createElement), configure (set id, textContent, etc.), and append (appendChild)....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-add-a-class-to-dom-element-in-javascript
How to Add a Class to DOM Element in JavaScript? - GeeksforGeeks
August 5, 2025 - <!DOCTYPE html> <html> <head> <style> .geekClass { background-color: green; text-align: center; font-size: 50px; } </style> </head> <body> <div id="gfg"> Geeks For Geeks </div> <button onclick="myClassName()">Add Class</button> <script> function myClassName() { let element = document.getElementById("gfg"); // Adding the class geekClass to element // with id gfg space is given in className // (" geekClass") as if there is already // a class attached to an element than our // new class won't overwrite and will append // one more class to the element element.className += " geekClass"; } </script> </body> </html> Output: Output ยท Comment ยท JavaScript Basics ยท
๐ŸŒ
W3Schools
w3schools.com โ€บ howto โ€บ howto_js_add_class.asp
How To Add a Class Name
<button onclick="myFunction()">Try it</button> <div id="myDIV"> This is a DIV element. </div> ... function myFunction() { var element = document.getElementById("myDIV"); element.classList.add("mystyle"); } Try it Yourself ยป ยท Tip: Also see How To Toggle A Class. Tip: Also see How To Remove A Class. Tip: Learn more about the classList property in our JavaScript Reference. Tip: Learn more about the className property in our JavaScript Reference. ... Coding fundamentals as bite-sized lessons and challenges.
๐ŸŒ
OOZOU
oozou.com โ€บ home โ€บ til โ€บ how to create an html element with attributes in one line in javascript
How to Create an HTML Element with Attributes in One Line in JavaScript | TIL | OOZOU
October 13, 2020 - By doing this, you can streamline ... individually. ... Object.assign(document.createElement('a'), { id: 'id', class: 'whatever class', href: 'https://www.google.com/', innerHTML: 'This is a link' });...
๐ŸŒ
CodeBrainer
codebrainer.com โ€บ home โ€บ how to createelement in javascript?
How to createElement in JavaScript? - CodeBrainer
July 16, 2024 - The first CSS is defined for the element with the id specialDiv (we define this with # in front of the id) and it makes fonts in this div red. This way it is apparent where a new element is created. If it is red, it was created inside specialDiv. Next is the CSS class colorBlue, all this class does is make the font color blue, again to make it apparent that we added a style using the createElement in JavaScript, followed by adding a CSS class to the new element..
๐ŸŒ
SoftAuthor
softauthor.com โ€บ home โ€บ javascript โ€บ 3 ways to create html element in javascript
3 Ways To Create HTML Element In JavaScript - SoftAuthor
September 20, 2022 - A JavaScript object in which we can use any of the DOM properties such as id, innerHTML, etc. document.body.appendChild( Object.assign( document.createElement('div'), { id : 'box'} ) ) This is easier than the previous approach if we have a lot ...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ javascript create element with class
How to Create Element With Class in JavaScript | Delft Stack
March 13, 2025 - The document.createElement method is a fundamental part of the JavaScript DOM API. This method allows you to create a new HTML element specified by the tag name. Once created, you can manipulate the element by adding attributes, classes, and styles.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ met_document_createelement.asp
HTML DOM Document createElement() Method
document.createElement() is a DOM Level 1 (1998) feature. ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial