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:
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("");
Tom Gullen
2010-08-10 15:27:55
+1 for not using jquery :-)
Hippo
2010-08-10 15:28:36
-∞. needs more jquery
Matt Ball
2010-08-10 15:31:51
Needs more cowbell!
Tom Gullen
2010-08-10 15:33:53
alternate jQuery: `$("#cart_item").empty();`
Rob Allen
2010-08-10 15:47:35
Could you use .detach() also?
RyanP13
2010-08-10 16:24:38
+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
2010-08-10 15:38:57