Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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

How can I do this?

like image 264
mrblah Avatar asked Mar 17 '09 03:03

mrblah


People also ask

How do I delete all content inside a div?

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.

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.

What jQuery method is used to completely remove attributes from elements?

To remove all attributes of elements, we use removeAttributeNode() method.


2 Answers

You want to use the empty function:

$('#mydiv').empty(); 
like image 183
Paolo Bergantino Avatar answered Sep 22 '22 17:09

Paolo Bergantino


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> 
like image 30
bart Avatar answered Sep 22 '22 17:09

bart