Open In App

HTML DOM createComment() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The createComment() method is used to create a comment node with some specified text. This property is used to set a text value as a parameter that is of string type. 

Syntax:

document.createComment( text )

Parameters: This method accepts single parameter text which is optional. This parameter is used to hold the comment string. 

Example: In this example, we will use createComment() method

html




<!DOCTYPE html>
<html>
   
<head>
    <title>DOM createComment() Method</title>
    <style>
        h1,
        h2 {
            color: green;
            font-weight: bold;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM createComment() Method</h2>
    <button onclick="geeks()">
      Submit
      </button>
    <p id="sudo"></p>
    <script>
        function geeks() {
            let c = document.createComment("GFG comments");
            document.body.appendChild(c);
            let x = document.getElementById("sudo");
            x.innerHTML = "Comments are Invisible!";
        }
    </script>
</body>
   
</html>


Output:

Supported Browsers: The browser supported by DOM createComment() Method properties are listed below:

  • Google Chrome 1
  • Edge 12
  • Firefox 1
  • Opera 12.1
  • Safari 1


Last Updated : 03 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads