views:

43

answers:

2

When the user clicks the button, onclick, the content of the div with id 'cart_item' should be cleared. This question may look silly. i am beginner to javascript. Plz help me

+2  A: 

alt text

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')" />

alt text

In JQuery (for reference)

If you prefer JQuery you could do:

$("#cart_item").html("");
Tom Gullen
+1 for not using jquery :-)
Hippo
-∞. needs more jquery
Matt Ball
Needs more cowbell!
Tom Gullen
alternate jQuery: `$("#cart_item").empty();`
Rob Allen
Could you use .detach() also?
RyanP13
+1  A: 

You can do it the DOM way as well:

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