My code is as follows:
g = document.createElement('div'); g.setAttribute("id", "divcontainer"); g.innerHTML = ` HTML GOES HERE `
When I use this on a website I want the div to be centered and visible, it is working because it creates the div (tested in console) but I can't see it.
I'm not using JQuery, but I can if needed. My goal is to have a UI type thing.
Your code only creates the element but does not add it to the DOM, for this you have to use
document.body.appendChild(element)
and add that element to the body element, you can also use The same method adds inner elements as well as elements selected by id or QuerySelector.You can modify the code as follows:
If you want to add multiple elements, you can use
append()
instead ofappendChild()
.document.body.append(g,g2,g3,g4)
Hope it helps!