71

Does any one know how to select HTML comment nodes with jQuery?

<html>
<head>
    <title>Check Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $("body *").each(function() {
                alert($(this).wrap("<span />").parent().html());
            });
        });
    </script>
</head>
<body>
    <!-- Hello -->  
    <p>
        <label for="thing">Thing Label</label>
        <input id="thing" type="checkbox" />
    </p>

This doesn't pick up the comment.

1
  • I think answer you marked isn't best. That is a good idea to marking second answer that has more vote. Commented Oct 15, 2016 at 16:53

4 Answers 4

118

This seems to do something equivalent to what you're looking for:

    $(function() {
        $("body").contents().filter(function(){
            return this.nodeType == 8;
        }).each(function(i, e){
            alert(e.nodeValue);
        });
    });
Sign up to request clarification or add additional context in comments.

6 Comments

This something doesnt always work, this one does: $("*").contents().filter(function(){ return this.nodeType == 8;})
@OskarAustegard - That was missing some code to work. Fixed: jsfiddle.net/zM5se/58
yeah, you're right, not sure how that ever worked. Maybe I included the wrong link - there are 58 versions of this one snippet...
nodeType Documentation at MDN
This doesn't work for me. I'm trying to detect <!--readmore--> comment but I'm not getting any.
|
14

There's the jQuery comments() plugin which will do that for you. Usage:

var comments = $( "#foo" ).comments();
alert(comments.html());

1 Comment

I really need to be able to select the comments in the same way as any other element, but thats a good start, thank you
13

And if you don't want a plugin:

var content = jQuery('body').html();
alert(content.match(/<!--.*?-->/g));

This uses regular expressions. It is set to search for anything enclosed by <!-- and --> while it doesn't matter what's written on the inside.

NOTE: I am not sure however, if jQuery also returns comments. If it does not, this approach does not work.

5 Comments

It does return comments, so the above works, thanks for the suggestion
Actually looking again, this is not realy using jquery to do anything useful ie document.body.innerHTML == jQuery('body').html(), and there is more to the regex than suggested here... thinking again, thanks anyway Mike
The regular expression was appreciated nonetheless by this party.
This won't select actual comment nodes. It will simply tell you if they are there.
This regex will only work for single line comments. To also handle comments that span lines, try this regex instead: /<!--[\s\S]*?-->/g
6

Once again, I'm late in the game, but I thought I'd contribute back as I found this post helpful in a recent situation I faced.

In our context, we have an ad service delivering blocks of code to render the ad.

Each ad has unique 'flight id'. Meaning the same 250x300 side rail ad can have multiple flights. So in one impression, you might see an ad for Subway, refresh and perhaps Quizno's.

Unfortunately, the service delivers this flight ID in a comment, and not as something a bit more useful like a data-attribute. That said, each comment is within a tag.

From the above, I was able to put together this solution to obtain the flight number in the comment leveraging JavaScript's exec() method of the RegExp object:

regexComment = new RegExp(/<!--\s*ad flight id:\s*([0-9]+)\s*-->/i);
targetElement = regexComment.exec($('div.advertisement').html());
if(targetElement.length > 0) {
    return parseInt(targetElement[1]);
}

Again, apologies for late in the game, but I thought it wouldn't hurt to provide yet another approach to this issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.