You should use the .setAttribute() method:

g = document.createElement('div');
g.setAttribute("id", "Div1");
Answer from lkaradashkov on Stack Overflow
๐ŸŒ
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

Can't select "dynamically" created element by id
๐ŸŒ forum.jquery.com
asp.net - jQuery create new div with ID? - Stack Overflow
The data element is the result you get back from you ajax call. You can search within that data to get the desired ID. Another option would be to create the div from jQuery with a simple insert or append call and create the id in the script. More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 4, 2014
Creating a div element in jQuery [duplicate]
It is error prone and hard to maintain, because the HTML-String can easily contain typos. Also, it mixes a markup language (HTML) with a programming language (Javascript/Jquery), which is usually a bad Idea. ... Save this answer. ... Show activity on this post. You can create separate tags ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
jquery - How to add an ID onto a created element in JavaScript? - Stack Overflow
My original question was marked falsely by another user as a duplicate but the link provided did not answer any parts of the question asked so I will ask another. Making the site builder I ran into... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 3, 2019
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ jquery โ€บ jquery-create-a-div-element
Create a Div Element using jQuery - GeeksforGeeks
January 29, 2024 - <!DOCTYPE html> <html> <head> <title> Create div element using jQuery </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <style> #parent { height: 100px; width: 300px; background: green; margin: 0 auto; } #newElement { height: 40px; width: 100px; margin: 0 auto; color: white } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <div id="parent"></div> <br><br> <button onclick="insertDiv()"> Insert Div </button> <script> function insertDiv() { $('<div id = "newElement">A Computer Science portal' + ' for geeks</div>').prependTo($('#parent')); } </script> </body> </html>
๐ŸŒ
W3Schools
w3schools.com โ€บ jquery โ€บ jquery_dom_add.asp
jQuery Add Elements
However, both the append() and prepend() methods can take an infinite number of new elements as parameters. The new elements can be generated with text/HTML (like we have done in the examples above), with jQuery, or with JavaScript code and DOM elements. In the following example, we create several ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-create-a-div-element-in-jquery
How to create a div element in jQuery ? | GeeksforGeeks
July 17, 2019 - To add attributes to an HTML element in jQuery, the attr() method can be used. This allows to set one or more attributes for a selected element, such as id, class, src, and href.Approach: Given an HTML document containing <label>, <input>, and ...
๐ŸŒ
jQuery
api.jquery.com โ€บ id-selector
ID Selector (โ€œ#idโ€) | jQuery API Documentation
Description: Selects a single element with the given id attribute.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-create-a-div-element-in-jquery.php
How to Create a DIV Element in jQuery
You can simply use the append() method to dynamically create an element such as a <div> at the end of the selected element in jQuery. Similarly, you can use the prepend() to create or insert element at the beginning.
๐ŸŒ
Coderwall
coderwall.com โ€บ p โ€บ evmqoa โ€บ creating-dom-elements-with-jquery-in-a-more-elegant-way
creating DOM elements with jQuery in a more elegant way (Example)
March 21, 2023 - So you are dynamically creating dom elements using jQuery? Do you hate html in you javascript code as much as I do? ... To me, this is slightly more elegant. ... It also makes it easier if you have dynamic properties, such as id created from a primary key, a conditional class. ... I think the point was to create a DOM element without writing to much html.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2020 โ€บ 07 โ€บ how-to-dynamically-create-div-in-jquery.html
How to dynamically create a div in jQuery? Example
July 3, 2023 - First, I am selecting our existing div by using ID selector i.e. $("#board") and then calling append() with HTML to add new divs. This method adds the new divs below the selected node.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-create-an-html-element-using-jquery
How to create an HTML element using jQuery ? | GeeksforGeeks
May 9, 2023 - To add attributes to an HTML element in jQuery, the attr() method can be used. This allows to set one or more attributes for a selected element, such as id, class, src, and href.Approach: Given an HTML document containing <label>, <input>, and ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-add-an-id-to-element-in-javascript
How to Add an ID to Element in JavaScript ? - GeeksforGeeks
July 23, 2025 - In JavaScript, the ID is the unique identifier through which the elements can be identified and manipulated if required. We can add an ID to an element in JavaScript using various approaches that are explored in this article with practical demonstration.