Open In App

HTML DOM comment() constructor

Last Updated : 15 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The comment() constructor in HTML DOM is used to return the newly created comment object with an optional parameter as a string. 

Syntax:

comment1 = new Comment(); // Create empty comment
comment2 = new Comment(String);

Parameters: This constructor accepts a single parameter string that holds the comment text. 

Example: This example creates a comment object with “This is a comment” as a parameter. 

HTML




<!DOCTYPE html>
<html>
   
<head>
    <title>
        HTML DOM comment() constructor
    </title>
    <style>
        a:focus {
            background-color: magenta;
        }
    </style>
</head>
   
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h2>HTML comment object</h2>
    <button onclick="getcomment ();">
        Get comment
    </button>
    <p id='comment'></p>
   
    <script type="text/javascript">
        function getcomment() {
            let comment =
                new Comment("This is a comment");
            console.log(comment);
        }
    </script>
</body>
 
</html>


Output:

 

Supported Browsers: The browsers supported by HTML DOM comment() constructor are listed below:

  • Google Chrome
  • Firefox 24
  • Opera


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads