Javascript examples for DOM:Document createElement
The createElement() method creates an Element Node with the specified name.
Parameter | Type | Description |
---|---|---|
nodename | String | Required. The name of the element you want to create |
An Element object, which represents the created Element node
The following code shows how to Create a paragraph element:
<!DOCTYPE html> <html> <head> <style> #myDIV {/* ww w . j ava2s .c o m*/ border: 1px solid black; margin-bottom: 10px; } </style> </head> <body> <div id="myDIV"> A DIV element </div> <button onclick="myFunction()">Test</button> <script> function myFunction() { var para = document.createElement("P"); var t = document.createTextNode("This is a paragraph."); para.appendChild(t); document.getElementById("myDIV").appendChild(para); } </script> </body> </html>