Jose Alvin Rañola Jose Alvin Rañola - 3 months ago 35
jQuery Question

jQuery wrap with html comment tags

I already tried 3 solution but didn't work.

I want to wrap my sidebar with comment tags.

First solution:

$("#sidebar").wrap("<!--googleoff: index--><!--googleon: index>");


Second solution:

From this post: jQuery comment/uncomment <!--element-->

function comment(element){
element.wrap(function() {
alert(this.outerHTML);
return '<!--googleoff: index-->'+this.outerHTML+'"<!--googleon: index>';
});
}
comment($('#sidebar'));


Third solution:

$('#sidebar').prepend("<!--googleoff: index-->");
$('#sidebar').append("<!--googleon: index>");


the result I want:

<!--googleoff: index--> //start comment tags
<div id="sidebar"></div>
<!--googleon: index> // tags

Answer

Try this.

$('#sidebar').before("<!--googleoff: index-->");
$('#sidebar').after("<!--googleon: index-->");