I have the following html:
<div id="container"><img src="some/path/image.jpg" /></div>
I want to remove that image by using a function. However, i'm not very experienced with javascript and I can't get it done because the image element has no ID or class.
$("button").click(function () {
$("container").remove("container > img");
});
However obviously that doesn't work. Please help!
I have the following html:
<div id="container"><img src="some/path/image.jpg" /></div>
I want to remove that image by using a function. However, i'm not very experienced with javascript and I can't get it done because the image element has no ID or class.
$("button").click(function () {
$("container").remove("container > img");
});
However obviously that doesn't work. Please help!
Share Improve this question asked Feb 2, 2012 at 1:32 Daan TwiceDaan Twice 3371 gold badge3 silver badges15 bronze badges 1- docs.jquery./Tutorials – user1106925 Commented Feb 2, 2012 at 1:43
4 Answers
Reset to default 7$("#container").find("img").remove();
that should do the trick.
Your code is perfect. However instead of just using the container, You should use # before it.
$("#container > img")
$("container") should be $("#container").
I would suggest $("#container").empty()
try this, could works...
$("button").click(function () {
//get the images
var myImages = $('#container').find('img');
for (var i =0; i< myImages.length; i++){
//remove each image
$('#container').removeChild(myImages[i]);
}
});