You are here: Reference > JavaScript > client-side > HTML DOM > methods > createComment (document, XMLDocument)

createComment method (document, XMLDocument)

Browser support:
Creates a CommentNode element from the specified text.
This method was introduced because CommentNode elements cannot be created by the createElement method.
When the new CommentNode element is created, use the insertBefore or appendChild method to insert it into the DOM hierarchy.

Syntax:

object.createComment (data);
You can find the related objects in the Supported by objects section below.

Parameters:

data
Required. String that specifies the text content of the CommentNode element. This content can be retrieved with the data property.

Return value:

Returns the newly created CommentNode object.

Example HTML code 1:

This example illustrates the use of the createComment method:
<head>
    <script type="text/javascript">
        function CreateComment () {
            var commentContainer = document.getElementById ("commentContainer");
            var comment = document.createComment ("Dynamically generated comment");
            commentContainer.appendChild (comment);
            alert (commentContainer.innerHTML);
        }
    </script>
</head>
<body>
    <div id="commentContainer"></div>
    <button onclick="CreateComment ();">Create a comment!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content