In jQuery I want to remove all HTML inside of a div

I have a div and I want to remove all the HTML inside of that div.

How can I do this?


Solution 1:

You want to use the empty function:

$('#mydiv').empty();

Solution 2:

I don't think empty() or html() is what you are looking for. I guess you're looking for something like strip_tags in PHP. If you want to do this, than you need to add this function:

jQuery.fn.stripTags = function() {
    return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') );
};

Suppose this is your HTML:

<div id='foo'>This is <b>bold</b> and this is <i>italic</i>.</div>

And then you do:

$("#foo").stripTags();

Which will result in:

<div id='foo'>This is bold and this is italic.</div>

Solution 3:

Another way is just to set the html to the empty string:

$('#mydiv').html('');