Skip to content

Instantly share code, notes, and snippets.

@dbergey
Created April 13, 2011 20:30
Show Gist options
  • Save dbergey/918337 to your computer and use it in GitHub Desktop.
Save dbergey/918337 to your computer and use it in GitHub Desktop.
// from: http://forum.jquery.com/topic/jquery-reading-comments-from-the-dom
jQuery.fn.comments = function(location) {
var location = location || "inside";
var comments = [];
this.each(function() {
if (location == "inside") {
var children = this.childNodes
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child.nodeType == 8) comments[comments.length] = $.trim(child.nodeValue);
}
} else if (location=="after") {
for (var next = this.nextSibling;next;next = next.nextSibling)
if (next.nodeType == 8) comments[comments.length] = $.trim(next.nodeValue);
} else if (location=="before") {
for (var prev = this.previousSibling; prev; prev = prev.previousSibling)
if (prev.nodeType == 8) comments[comments.length] = $.trim(prev.nodeValue);
}
});
return comments;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment