Morgan Cheng Morgan Cheng - 2 months ago 5x
Javascript Question

Is it possible to get reference to comment element/block by JavaScript?

This sounds a little crazy, but I'm wondering whether possible to get reference to comment element so that I can dynamically replace it other content with JavaScript.

<div id="header"></div>
<div id="content"></div>
<!-- sidebar place holder: some id-->

In above page, can I get reference to the comment block and replace it with some content in local storage?

I know that I can have a div place holder. Just wondering whether it applies to comment block.

var findComments = function(el) {
    var arr = [];
    for(var i = 0; i < el.childNodes.length; i++) {
        var node = el.childNodes[i];
        if(node.nodeType === 8) {
        } else {
            arr.push.apply(arr, findComments(node));
    return arr;

var commentNodes = findComments(document);

// whatever you were going to do with the comment...