1var element = document.createElement("span");
2element.setAttribute("style", "color: red");
3document.body.appendChild(element);
1var newDiv = document.createElement("div");
2document.body.appendChild(newDiv);
1document.body.onload = addElement;
2
3function addElement () {
4 // create a new div element
5 var newDiv = document.createElement("div");
6 // and give it some content
7 var newContent = document.createTextNode("Hi there and greetings!");
8 // add the text node to the newly created div
9 newDiv.appendChild(newContent);
10
11 // add the newly created element and its content into the DOM
12 var currentDiv = document.getElementById("div1");
13 document.body.insertBefore(newDiv, currentDiv);
14}
1const element = document.createElement("h1"); /* First need to make element to edit it! */
2element.setAttribute("style", text-align:center"); /* First use Property then next its value
3document.body.appendChild(element); /* Used to show the element in body tag cuz other tags doesn't have display, see it in DevTools! */
1/* Oh Sorry, in the previous answer is posted by me but in line 2, I've a problem, see this fixed: */
2const element = document.createElement("h1"); /* First need to make element to edit it! */
3element.setAttribute("style", /* This */ "text-align:center"); /* Here have a problem, I haven't included " in here*/
4document.body.appendChild(element); /* Used to show the element in body tag cuz other tags doesn't have display, see it in DevTools! */