Skip to main content

How come this JQuery code returns nothing?





<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

<script>

var newvalue = '<img class="youtube_replace youtube_canvas" data-code="Wn-_MyJV37E" src="http://img.youtube.com/vi/Wn-_MyJV37E/0.jpg" />';



var abc = $('<div>' + newvalue + '</div>').find('*').each(function() {

}).html();



alert(abc);



</script>







I want abc to equal "newvalue". But in my current code, abc is empty. Why?





This is what I truly want to do, but for example purposes, I left this blank above:







var whitelist = ['a','div','img', 'span'];

var abc = $('<div>' + newvalue + '</div>').find('*').each(function() {

if($.inArray(this.nodeName.toLowerCase(), whitelist)==-1) {

$(this).remove();

}

}).html(); //abc is now sanitized!!!




Comments