How to remove a container that is attached to the body


sdvnksv

I created a container and added this to the body:

// Create container
var container = "<div class='foo'> ... </div>";

// Prepend it to the body
$("body").prepend(container);

Now, I want to fade out the container after a few seconds:

setTimeout(function() {
    container.fadeOut();
}, 3000);

However, it says undefined is not a function. I'm sure $(".foo").fadeOut()it works, but I have a lot of these .foocontainers and I don't want to assign each container a separate ID.

Dandavis

Make your containervar point to a jq object instead of a string:

var container = $('<div class="foo"> ... </div>');

// Prepend it to the body
$("body").prepend(container);

setTimeout(function() {
   container.fadeOut();
}, 3000);

Now, instead of a silly string , containerthere's a method.fadeOut()

EDIT: Per request, the original version of the creation section:

var container = document.createElement("div");
container.className="foo";
container.innerHTML= "<b>Hello world</b>";
document.body.insertBefore(container, document.body.firstChild);

The fadeOut part in vanilla:

<style> 
        div.foo{ opacity: 1; transition: 1000ms opacity;}
        div.foo.fade { opacity: 0; }
</style>

  setTimeout(function(){ container.classList.add("fade");}, 3000);

Related


How to remove a container that is attached to the body

sdvnksv I created a container and added this to the body: // Create container var container = "<div class='foo'> ... </div>"; // Prepend it to the body $("body").prepend(container); Now, I want to fade out the container after a few seconds: setTimeout(functi

Docker - How to access a volume not attached to a container?

Matt Bryson I have a data container that has a volume (--volumes-from) used by other containers. The data container was accidentally deleted. Fortunately, the volume was not deleted. Is there any way to rerun the data container and point it to that volume? Fen

Docker - How to access a volume not attached to a container?

Matt Bryson I have a data container that has a volume (--volumes-from) used by other containers. The data container was accidentally deleted. Fortunately, the volume was not deleted. Is there any way to rerun the data container and point it to that volume? Fen

Docker - How to access a volume not attached to a container?

Matt Bryson I have a data container that has a volume (--volumes-from) used by other containers. The data container was accidentally deleted. Fortunately, the volume was not deleted. Is there any way to rerun the data container and point it to that volume? Fen

Docker - How to access a volume not attached to a container?

Matt Bryson I have a data container that has a volume (--volumes-from) used by other containers. The data container was accidentally deleted. Fortunately, the volume was not deleted. Is there any way to rerun the data container and point it to that volume? Fen

How to remove the attached div on button click?

username I've seen a lot of examples here, but I'm facing another problem here, the event is not even detected (the button in the attached div). I use the append method on the button click to add some textboxes and buttons and it works fine. But when I try to

How to remove the attached div on button click?

username I've seen a lot of examples here, but I'm facing another problem here, the event is not even detected (the button in the attached div). I use the append method on the button click to add some textboxes and buttons and it works fine. But when I try to

How to remove attached ``div'' jQuery Codeigniter

it I want to delete the attached item. please help. $(document).ready(function() { $("#appendex0").click(function() { $(".divcls0").append('<div class="col-sm-10 col-sm-offset-1"><div class="col-sm-3 col-sm-offset-1"><div class="form-group"><div class="n

How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

How to remove the attached div on button click?

username I've seen a lot of examples here, but I'm facing another problem here, the event is not even detected (the button in the attached div). I use the append method on the button click to add some textboxes and buttons and it works fine. But when I try to

How to remove attached ``div'' jQuery Codeigniter

it I want to delete the attached item. please help. $(document).ready(function() { $("#appendex0").click(function() { $(".divcls0").append('<div class="col-sm-10 col-sm-offset-1"><div class="col-sm-3 col-sm-offset-1"><div class="form-group"><div class="n

How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

how to remove all body from body

Wu Guanghua I'm working on a corona project and now I want to remove all objects from physics. I see it only has one way to remove corpses, but not all physics.removeBody() I need to delete everything physics.removeAllBodies() Can anyone give me a way to do

how to remove all body from body

Wu Guanghua I'm working on a corona project and now I want to remove all objects from physics. I see it only has one way to remove corpses, but not all physics.removeBody() I need to delete everything physics.removeAllBodies() Can anyone give me a way to do