How to clear all <div>s’ contents inside a parent <div>?

Prasad picture Prasad · Nov 9, 2009 · Viewed 373k times · Source

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?

Answer

Emil Ivanov picture Emil Ivanov · Nov 9, 2009

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.