Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to remove an html comment from dom using jquery

Tags:

jquery

just wondering if there was a way to remove an html comment using jquery.

<!-- <div id="main">Some text </div> -->

thanks

like image 767
akano1 Avatar asked Mar 02 '10 16:03

akano1


People also ask

How can you remove and HTML element using jQuery?

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.

Does jQuery manipulate the DOM?

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.

How do you delete something in jQuery?

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 .


1 Answers

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.

like image 183
user113716 Avatar answered Sep 23 '22 09:09

user113716