The createComment() method creates a Comment node with the specified text.
Syntax and Usage
document.createComment(text)
Parameter Values
text String Optional. The text you want to be your comment, in the Comment object
Example(1)
<script>
function drop_comment() {
var c = document.createComment("My personal comments");
document.body.appendChild(c);
var x = document.getElementById("demo");
x.innerHTML = "A comment was added to this document, but as you know, comments are invisible.";
}
</script>
In above example creating a comment node, and insert it to the HTML document.
Complete Code For HTML DOM CreateComment Method In JavaScript
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM CreateComment Method In JavaScript</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<br>
<div class="text-center">
<h1>HTML DOM CreateComment Method In JavaScript</h1>
</div>
<br>
<div class="well">
<h1 id="demo"></h1>
<button class="btn btn-primary" onclick="drop_comment()">Click it</button>
</div>
<br>
</div>
</body>
</html>
<script>
function drop_comment() {
var c = document.createComment("My personal comments");
document.body.appendChild(c);
var x = document.getElementById("demo");
x.innerHTML = "A comment was added to this document, but as you know, comments are invisible.";
}
</script>