{"id":1095,"date":"2012-06-11T12:35:15","date_gmt":"2012-06-11T18:35:15","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1095"},"modified":"2012-06-12T00:07:35","modified_gmt":"2012-06-12T06:07:35","slug":"code-of-the-day-jquery-get-comments-from-html-elements","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/06\/code-of-the-day-jquery-get-comments-from-html-elements\/","title":{"rendered":"Code of the day: jQuery, Get comments from HTML elements."},"content":{"rendered":"<p>Code of the day: Get comments from HTML elements.<br \/>\nHere&#8217;s a simple jquery plugin to get the comments from a HTML elements.<br \/>\n<code><\/p>\n<pre lang='javascript'>\r\n\/\/ source code for $.fn.getComments()\r\n$(function () {\r\n\/**\r\n* $.fn.getComments() is used to extract the html comments from a HTML elements.\r\n* \r\n* @author Larry Battle <http:\/\/bateru.com\/news\/contact-me>\r\n* @license MIT\r\n* @date June 11, 2012\r\n* @version 0.1\r\n* @args {boolean} asArray - If true, returns an array of the comments values. \r\n\t\tOtherwise returns jquery objects of the node comments.\r\n* @example \r\n\tHTML:\r\n\t<div id=\"example\">I am a div. <!--Duh!--><\/div>\r\n\r\n\tJavascript: \r\n\t$(\"#example\").getComments(true) \/\/ returns [ \"Duh!\" ]\r\n*\/\r\n\tvar getCommentsFromEl = function (el, asArray) {\r\n\t\tvar result,\r\n\t\t$el = $(el).contents();\r\n\t\tresult = $el.filter(function () {\r\n\t\t\t\treturn this.nodeType == 8;\r\n\t\t\t});\r\n\t\tif (asArray) {\r\n\t\t\tresult = $.makeArray(result.map(function () {\r\n\t\t\t\t\t\treturn this.nodeValue;\r\n\t\t\t\t\t}));\r\n\t\t}\r\n\t\treturn result;\r\n\t};\r\n\t$.fn.getComments = function (asArray) {\r\n\t\treturn getCommentsFromEl(this, asArray);\r\n\t};\r\n});\r\n<\/pre>\n<p><\/code><\/p>\n<p>Demo and testcases here: <a href=\"http:\/\/jsfiddle.net\/96rux\/\">http:\/\/jsfiddle.net\/96rux\/<\/a><\/p>\n<p><iframe style=\"width: 100%; height: 300px\" src=\"http:\/\/jsfiddle.net\/96rux\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<p>Fork this on Github.com<br \/>\n<a href=\"https:\/\/github.com\/LarryBattle\/jQuery.getComments\">https:\/\/github.com\/LarryBattle\/jQuery.getComments<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code of the day: Get comments from HTML elements. Here&#8217;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 &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/06\/code-of-the-day-jquery-get-comments-from-html-elements\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day: jQuery, Get comments from HTML elements.<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10],"tags":[30,145,189,164,5,16],"class_list":["post-1095","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials","tag-code-of-the-day","tag-comments","tag-github","tag-javascript","tag-jquery","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1095"}],"version-history":[{"count":5,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1095\/revisions"}],"predecessor-version":[{"id":1101,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1095\/revisions\/1101"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}