Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear the content of a div using JavaScript? [closed]

People also ask

How do I clear the content of a div using JavaScript?

JavaScript provides the functionality of clearing the content of div. There are two methods to perform this function, one by using innerHTML property and other by using firstChild property and removeChild() method.

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 clear an element in HTML?

HTML elements with no content are called empty elements. <br> is an empty element without a closing tag (the <br> tag defines a line break). Empty elements can be "closed" in the opening tag like this: <br />. Elements with no closing tag are known as an empty tag.


Just Javascript (as requested)

Add this function somewhere on your page (preferably in the <head>)

function clearBox(elementID)
{
    document.getElementById(elementID).innerHTML = "";
}

Then add the button on click event:

<button onclick="clearBox('cart_item')" />

In JQuery (for reference)

If you prefer JQuery you could do:

$("#cart_item").html("");

You can do it the DOM way as well:

var div = document.getElementById('cart_item');
while(div.firstChild){
    div.removeChild(div.firstChild);
}