When the user clicks a button on my page, the content of a div should be cleared. How would I go about accomplishing this?
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("");
alternate jQuery:
$("#cart_item").empty();
Could you use .detach() also?
@Tom Gullen this worked for me without passing the id of the DIV into the function, and with single quotes around the elementID in the function itself. Thanks for this!
@Tom Gullen : please not that this does'nt remove event handlers associated with the nodes inside that div.
@Mic's answer is about 250x quicker. jsperf.com/innerhtml-vs-removechild