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

0 votes

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?

Oct 7 in Java-Script by kartik
• 36,020 points
20 views

1 answer to this question.

0 votes

Hello @kartik,

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.

Hope it helps!!

Thank you!!

answered Oct 7 by Niroj
• 78,480 points

Related Questions In Java-Script

0 votes
1 answer

How to replace all occurrences of a string?

Hello @kartik, As an alternative to regular expressions ...READ MORE

answered Aug 28 in Java-Script by Niroj
• 78,480 points
57 views
0 votes
1 answer

How to call a parent window function from an iframe?

Hello @kartik, Using Window.postMessage() This method safely enables cross-origin communication. And if ...READ MORE

answered Sep 3 in Java-Script by Niroj
• 78,480 points
177 views
0 votes
1 answer

How can I make a div stick to the top of the screen once it's been scrolled to?

Hello @kartik, Using javascript: var initTopPosition= $('#myElementToStick').offset().top; ...READ MORE

answered Sep 4 in Java-Script by Niroj
• 78,480 points
35 views
0 votes
1 answer

How to remove all line breaks from a string?

Hello @kartik, Windows would be \r\n, but Linux just ...READ MORE

answered Sep 18 in Java-Script by Niroj
• 78,480 points
185 views
0 votes
1 answer

How to access PHP var from external javascript file?

Hello @kartik, You don't really access it, you ...READ MORE

answered Jul 6 in Java-Script by Niroj
• 78,480 points
870 views
0 votes
1 answer

How do I escape a single quote in SQL Server?

Hello @kartik, Single quotes are escaped by doubling ...READ MORE

answered Jul 21 in PHP by Niroj
• 78,480 points
982 views
0 votes
1 answer

How to specify a port to run a create-react-app based project?

Hello @kartik, You could use cross-env to set the port, ...READ MORE

answered Jul 22 in Angular by Niroj
• 78,480 points
940 views
0 votes
1 answer

How do you set a default value for a MySQL Datetime column?

Hello @kartik, In version 5.6.5, it is possible ...READ MORE

answered Aug 18 in PHP by Niroj
• 78,480 points
438 views
0 votes
1 answer

How jQuery event to trigger action when a div is made visible?

Hello @kartik, You could always add to the ...READ MORE

answered Aug 25 in Java-Script by Niroj
• 78,480 points
59 views
0 votes
1 answer

How to disable all div content?

Hello @kartik, A simple way to disable any ...READ MORE

answered Aug 25 in Java-Script by Niroj
• 78,480 points
52 views