EN
JavaScript - create comment node dynamically
0
points
In this article, we would like to show you how to create a comment node dynamically using JavaScript.
Quick solution:
let comment = document.createComment('Comment text...');
Practical example
In this example, we use document.createComment()
to create a comment dynamically and then append it to the document.body
. When we log
the body.outerHTML
in the console, you can see the comment tag at the end of the body
section.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<script>
const comment = document.createComment('This is example comment...');
document.body.appendChild(comment);
console.log(document.body.outerHTML);
</script>
</body>
</html>