createElement() Method
Menu createElement() Method
 

How to use the HTML DOM createElement() Method

As it says on the tin, this is a DOM Method to create an Element. Another method seen when analysing the code for viewing html on an iPad.

The examples on the W3C website and those you can see their operation in their Tryit Editor the created Elements are appended to the body element. I changed the code to append to an element with an id specified in the code.

Top

The (modified) W3C example

button onclick="myButton()">Try it//button>
<script>
function myButton() {
  var btn = document.createElement("BUTTON");
  btn.innerHTML = "CLICK ME";
  document.getElementById("myBUT").appendChild(btn);     
}
</script>

Click the button to make a BUTTON element with text.  

Need to add some more formatting for the new button


The 2nd (modified) W3C example

of the use of createElement()

<script>
function myText() {
  var para2 = document.createElement("P");
  para2.innerText = "This is a paragraph.";
document.getElementById("myDIV").appendChild(para2);     
}
/script>

Click the button to create a P element with some text.

Site design by Tempusfugit Web Design -