I have a div and I want to remove all the HTML inside of that div.
How can I do this?
Given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To remove elements and its content, jQuery provides two methods: remove(): It removes the selected element with its child elements. empty(): It removes the child element from the selected elements.
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.
To remove all attributes of elements, we use removeAttributeNode() method.
You want to use the empty function:
$('#mydiv').empty();
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>
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