Latest web development tutorials

HTML DOM createElement () method

Document Object Reference Document Object

Examples

Create a button:

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

Output:


try it"

HTML elements often contain text. Create the specified text buttons you need to add the button element text node:

Examples

Create button to specify the text:

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

Output:


try it"

Definition and Usage

createElement () method creates an element by specifying a name


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support createElement () method


grammar

document.createElement( nodename )

parameter

参数 类型 描述
nodename String 必须。创建元素的名称。

return value

类型 描述
元素对象 创建的元素节点

technical details

DOM version Core Level 1 Document Object


Document Object Reference Document Object