Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to empty the content of a div

Does someone know how to empty the content of a div (without destroying it) in JavaScript?

Thanks,

Bruno

like image 859
Bruno Avatar asked Apr 21 '11 12:04

Bruno


People also ask

How do you clear HTML?

The <input> tag, helps you to take user input using the type attribute. To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How check Div is empty or not in jQuery?

Method 1: Using the “:empty” selector: The element to be checked using is() method. The is() method is used to check if one of the selected elements matches to the selector element. This method can be used on this element to test if it is empty by using “:empty” selector.


2 Answers

If your div looks like this:

<div id="MyDiv">content in here</div>

Then this Javascript:

document.getElementById("MyDiv").innerHTML = "";

will make it look like this:

<div id="MyDiv"></div>

like image 96
Zach Rattner Avatar answered Sep 22 '22 01:09

Zach Rattner


If you're using jQuery ...

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

or

$('div').empty(); 
like image 31
ezmilhouse Avatar answered Sep 26 '22 01:09

ezmilhouse