Skip to content

Instantly share code, notes, and snippets.

@dave1010
Created October 10, 2011 15:40
Show Gist options
  • Save dave1010/1275643 to your computer and use it in GitHub Desktop.
Save dave1010/1275643 to your computer and use it in GitHub Desktop.
jQuery plugin: set the HTML of an element to be the contents of its first comment
/*global $ */
/**
* Replace an element's HTML with the content of its first comment
*/
$.fn.commentToHTML = function() {
var d = 'extracted-comments';
return this.each(function(i, el) {
var $el = $(this);
if ($el.data(d)) {
// comments have already been extracted
// no need to do it again
return;
}
var node = el.firstChild;
while (node) {
// found a HTML comment
if (node.nodeType === 8) {
$el
.html(node.nodeValue)
.data(d, true);
return;
}
node = node.nextSibling;
}
});
};
@dave1010
Copy link
Author

Question: Why is this useful?

Answer: http://calendar.perfplanet.com/2010/html-lazy-load/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment