Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an element's content with JQuery?

Tags:

Say I have a div and some content inside it.

<div>       Content </div> 

With JQuery, how do I empty the div without removing the div, only the content inside?

like image 796
Keith Donegan Avatar asked Sep 24 '09 22:09

Keith Donegan


People also ask

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 .

Which jQuery method is used to remove the child elements from the selected element?

jQuery remove() Method The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.

How do I remove all elements from a div?

The empty() method removes all child nodes and content from the selected elements.

How do you delete an element in HTML?

Approach: Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.


2 Answers

You can use the empty function to remove all the child nodes (all its content) of an element:

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

The empty function will also remove all the event handlers and the jQuery internally cached data.

like image 127
Christian C. Salvadó Avatar answered Sep 22 '22 04:09

Christian C. Salvadó


If the div has an id, you can do it like this:

$('#id-of-div').html(''); 

Or you can do all classes of .class-of-div

$('.class-of-div').html(''); 

Or just all divs

$('div').html(''); 

EDIT: But empty() (above) would work better.

like image 39
davethegr8 Avatar answered Sep 23 '22 04:09

davethegr8