Get HTML comments

by scurrilus

HTML

<div id="container">
  <!-- This is a comment -->
  <p>Some text here</p>
  <!-- Another comment -->
  <div>More content</div>
</div>

JavaScript

const container = document.getElementById('container');
const comments = Array.from(container.childNodes).filter(node => node.nodeType === Node.COMMENT_NODE);

comments.forEach(comment => {
  console.log(comment.textContent); // This will print the comment content
});