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

Rajasekar picture Rajasekar · Aug 10, 2010 · Viewed 490.8k times · Source

When the user clicks a button on my page, the content of a div should be cleared. How would I go about accomplishing this?

Answer

Tom Gullen picture Tom Gullen · Aug 10, 2010

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("");