just wondering if there was a way to remove an html comment using jquery.
<!-- <div id="main">Some text </div> -->
thanks
To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.
Projects In JavaScript & JQueryjQuery provides a number of methods to manipulate DOM in efficient way. You do not need to write big and complex code to set or get the content of any HTML element.
Use . remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .
Try this:
$('*').contents().each(function() {
if(this.nodeType === Node.COMMENT_NODE) {
$(this).remove();
}
});
EDIT: This removes the elements from the DOM. Browsers often store a copy of the original page source that is accessible through a menu item. This doesn't get updated.
If you want to hide your comments, you could always insert your entire HTML markup (with comments) into the DOM using javascript. The javascript could, of course, be viewed, but it is a step removed from the first place people would look.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With