Code of the day: Get comments from HTML elements.
Here’s a simple jquery plugin to get the comments from a HTML elements.
// source code for $.fn.getComments()
$(function () {
/**
* $.fn.getComments() is used to extract the html comments from a HTML elements.
*
* @author Larry Battle
* @license MIT
* @date June 11, 2012
* @version 0.1
* @args {boolean} asArray - If true, returns an array of the comments values.
Otherwise returns jquery objects of the node comments.
* @example
HTML:
I am a div.
Javascript:
$("#example").getComments(true) // returns [ "Duh!" ]
*/
var getCommentsFromEl = function (el, asArray) {
var result,
$el = $(el).contents();
result = $el.filter(function () {
return this.nodeType == 8;
});
if (asArray) {
result = $.makeArray(result.map(function () {
return this.nodeValue;
}));
}
return result;
};
$.fn.getComments = function (asArray) {
return getCommentsFromEl(this, asArray);
};
});
Demo and testcases here: http://jsfiddle.net/96rux/
Fork this on Github.com
https://github.com/LarryBattle/jQuery.getComments
What REALLY is Data Science? Told by a Data Scientist - By Joma Tech
Writing perfect code is a challenging process. That's where code reviews come in to help…
"The Next Leap: How A.I. will change the 3D industry - Andrew Price - Blender"
"Captain Disillusion: World's Greatest Blenderer - Live at the Blender Conference 2018 - CaptainDisillusion"
My 5 Favorite Linux Shell Tricks for SPEEEEEED (and efficiency) - By tutoriaLinux > What's…