228

I have a div <div id="masterdiv"> which has several child <div>s.

Example:

<div id="masterdiv">
  <div id="childdiv1" />
  <div id="childdiv2" />
  <div id="childdiv3" />
</div>

How to clear the contents of all child <div>s inside the master <div> using jQuery?

allenski
  • 1,652
  • 4
  • 23
  • 39
Prasad
  • 58,881
  • 64
  • 151
  • 199

14 Answers14

468

jQuery's empty() function does just that:

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

clears the master div.

$('#masterdiv div').empty();

clears all the child divs, but leaves the master intact.

Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90
  • 8
    http://api.jquery.com/empty/ it is true but i dont know why Quentin answer accepted :) – Nuri YILMAZ Jul 03 '11 at 19:29
  • 5
    This clears more than what was asked for in the original question -- you should use a more [specific selector](http://stackoverflow.com/questions/1701973/clear-all-divs-contents-inside-a-div/3543068#3543068). – Drew Noakes Aug 10 '11 at 10:48
  • 4
    Wow, thanks you Drew for pointing this out 1.5 years later :) Fixed. – Emil Ivanov Aug 10 '11 at 11:04
280
jQuery('#masterdiv div').html('');
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Can we clone the html and manipulate on that? like var tmp = $('
    ').append($('#masterdiv').clone()).remove().html(); and get the divs inside #masterdiv of tmp and clear the contents and return
    – Prasad Nov 10 '09 at 10:24
  • 119
    Using `.empty()` would be a better option as no string parsing is involved. It operates directly on the DOM object model. – Drew Noakes Aug 22 '10 at 19:43
  • 7
    `empty()` also clears all the content that is generated with jQuery. – MikkoP Feb 03 '14 at 15:32
23

Use jQuery's CSS selector syntax to select all div elements inside the element with id masterdiv. Then call empty() to clear the contents.

$('#masterdiv div').empty();

Using text('') or html('') will cause some string parsing to take place, which generally is a bad idea when working with the DOM. Try and use DOM manipulation methods that do not involve string representations of DOM objects wherever possible.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
20

I know this is a jQuery related question, but I believe someone might get here expecting a pure Javascript solution. So, if you were trying to do this using js, you could use the innerHTML property and set it to an empty string.

document.getElementById('masterdiv').innerHTML = '';
Alain Cruz
  • 4,757
  • 3
  • 25
  • 43
  • 4
    this was the top result on Google when searching for how to empty a div. Thank you! – Prid Apr 18 '20 at 07:04
14

jQuery recommend you use ".empty()",".remove()",".detach()"

if you needed delete all element in element, use this code :

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

if you needed delete all element, Use this code:

$('#target_id').remove();

i and jQuery group not recommend for use SET FUNCTION like .html() .attr() .text() , what is that? it's IF YOU WANT TO SET ANYTHING YOU NEED

ref :https://learn.jquery.com/using-jquery-core/manipulating-elements/

Abdul Aziz Al Basyir
  • 1,104
  • 13
  • 28
12

If all the divs inside that masterdiv needs to be cleared, it this.

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

else, you need to iterate on all the div children of #masterdiv, and check if the id starts with childdiv.

$('#masterdiv div').each(
    function(element){
        if(element.attr('id').substr(0, 8) == "childdiv")
        {
            element.html('');
        }
    }
 );
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
Ikke
  • 99,403
  • 23
  • 97
  • 120
11

The better way is :

 $( ".masterdiv" ).empty();
Joe Mike
  • 1,150
  • 1
  • 9
  • 16
  • 1
    This selects all elements with class "masterdiv", not id "masterdiv", as per the questions - just a note. – Dave Aug 16 '18 at 07:56
7
$("#masterdiv div").text("");
brendan
  • 29,308
  • 20
  • 68
  • 109
6
$("#masterdiv > *").text("")

or

$("#masterdiv").children().text("")
ase
  • 13,231
  • 4
  • 34
  • 46
6
$('#div_id').empty();

or

$('.div_class').empty();

Works Fine to remove contents inside a div

SysDragon
  • 9,692
  • 15
  • 60
  • 89
Raki
  • 535
  • 4
  • 13
6

You can use .empty() function to clear all the child elements

 $(document).ready(function () {
  $("#button").click(function () {
   //only the content inside of the element will be deleted
   $("#masterdiv").empty();
  });
 });

To see the comparison between jquery .empty(), .hide(), .remove() and .detach() follow here http://www.voidtricks.com/jquery-empty-hide-remove-detach/

Anand Roshan
  • 335
  • 4
  • 2
5

When you are appending data into div by id using any service or database, first try it empty, like this:

var json = jsonParse(data.d);
$('#divname').empty();
Pang
  • 9,564
  • 146
  • 81
  • 122
Sheeraz
  • 59
  • 1
  • 4
4
$("#masterdiv div[id^='childdiv']").each(function(el){$(el).empty();});

or

$("#masterdiv").find("div[id^='childdiv']").each(function(el){$(el).empty();});
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
0

try them if it help.

$('.div_parent .div_child').empty();

$('#div_parent #div_child').empty();

Gaurav Kaushik
  • 187
  • 1
  • 1
  • 12