createElement()

The createElement() method creates an element node.

Syntax

document.createElement()
 

Parameters

Parameter

Description

type

Required.
The type of element to create.

Return Value

Type

Description

Node

The created element node.


createTextNode()

The createTextNode() method creates a text node.

Syntax

document.createTextNode(text)

Parameters

Parameter Description
text Required.
The text for the node.

Return Value

Type Description
Node The created text node.

createComment()

The createComment() method creates a comment and returns the comment node.

Syntax

document.createComment(text)

Parameters

Parameter Description
text Optional.
The comment text.

Return Value

Type Description
Node The created comment node.


html file

<!DOCTYPE html>
<html>
<head>
    <title>DOM Navigation</title>
    <style>
        h1{
            text-align: center;
            color:#ff0000;
        }
    </style>
</head>
<body>
    <h1>Yahoo Baba : DOM Create Methods</h1>
    
    <script src="js/dom-create.js"></script>
</body>
</html>

dom-create.js

//var newElement = document.createElement("p");

var newElement = document.createElement("h2");

console.log(newElement);

var newText = document.createTextNode("This is just text");

console.log(newText);

/* Dom Create  Comment*/

var newComment = document.createComment("this is comment");

console.log(newComment);