Comment Node

by HYEONGJINKIM

HTML

<div class="example">I am a div. <!--i am a comment! 2014년 2월 13일 수정--></div>
<div class="example">I am a div. <!--i am a comment! 2014년 2월 14일 수정--></div>
<div class="example">I am a div. <!--i am a comment! 2014년 2월 15일 수정--></div>
<div class="example">I am a div. <!--i am a comment! 2014년 2월 16일 수정--></div>

CSS

.editor { position:absolute; top:0px; right: 0px; width: 300px; padding:20px; color:#FFF; background: #930; }

JavaScript

$(function () { 
    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 () {
                console.log(this.nodeValue);
                        return this.nodeValue + '</br>';
            })).join('');
        }
        return result;
    };
    $.fn.getComments = function (asArray) {
        return getCommentsFromEl(this, asArray);
    };
	
	var sComment = $(".example").getComments(true);
	
	$('body').append('<p class="editor">' +  sComment+ '</p>')
	
});