Javascript Reference - HTML DOM createComment() Method








The createComment() method creates a Comment node from the specified text.

Browser Support

createComment Yes Yes Yes Yes Yes

Syntax

document.createComment(text)

Parameters

The optional text is a String type value representing text content in the Comment object.





Return Value

A Comment object.

Example

The following code shows how to create a comment node, and insert it to the HTML document.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from   w w  w .j  a  v  a2s. c  o m-->
<script>
function myFunction()
{
    var c = document.createComment("My personal comments");
    document.body.appendChild(c);
    var x = document.getElementById("demo");
    x.innerHTML = "comments";
}
</script>

</body>
</html>

The code above is rendered as follows: