HTML - DOM Document createComment() Method
The createComment() method is used for creating a comment node with the given text. Since comments are not visible you have to inspect the HTML document after executing this method to see the comment created.
Syntax
document.createComment(text);
Parameter
This method accepts a single parameters as listed below.
| Parameter | Description |
|---|---|
| text | It represents comment which you want to add in the document.It is optional. |
Return Value
It returns the created comment node.
Example of HTML DOM Document 'createComment()' Method
Here is an example of using createComment() method to add a comment in HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<title>
HTML DOM document createComment() Method
</title>
</head>
<body>
<h3>HTML DOM document createComment() Method</h3>
<button onclick="fun()">Click me</button>
<p id="comment"></p>
<script>
function fun() {
let x = document.createComment("This is a sample Comment");
document.body.appendChild(x);
let p = document.getElementById("comment");
p.innerHTML = "The comment was added to this HTML document but is invisible";
}
</script>
</body>
</html>
Supported Browsers
| Method | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| createComment() | Yes 1 | Yes 12 | Yes 1 | Yes 1 | Yes 12.1 |
html_dom.htm
Advertisements




