X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

HTML DOM createElement() Method

Document Object Reference Document Object

Example

Create a button:

var btn = document.createElement("BUTTON");

The result will be:


Try it yourself »

HTML elements often contains text. To create a button with text you must also create a Text Node which you append to the button element:

Example

Create a button with text:

var btn = document.createElement("BUTTON");
var t = document.createTextNode("CLICK ME");
btn.appendChild(t);

The result will be:


Try it yourself »

More "Try it Yourself" examples below.


Definition and Usage

The createElement() method creates an Element Node with the specified name.

Tip: Use the createTextNode() method to create a text node.


Browser Support

Method
createElement() Yes Yes Yes Yes Yes


Syntax

document.createElement(nodename)

Parameter Values

Parameter Type Description
nodename String Required. The name of the element you want to create

Technical Details

DOM Version: Core Level 1 Document Object
Return Value: An Element object, which represents the created Element node


Examples

More Examples

Example

Create a <p> element:

var x = document.createElement("P");
var t = document.createTextNode("This is a paragraph");
x.appendChild(t);

Try it yourself »

Example

Create a <p> element and append it to a <div> element:

var x = document.createElement("P");
var t = document.createTextNode("This is a paragraph.");
x.appendChild(t);
document.getElementById("myDIV").appendChild(x);

Try it yourself »


Document Object Reference Document Object


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook