How to unload the HTML page loaded in div tagafter 5 sec using Jquery?

2514 views javascript
3

I have following div in my page with some css. with this I have few div on top and below it.

HTML:

  <div id="sentdiv"></div>

CSS:

#sentdiv{
    margin-left: 13rem;
    margin-right: 20rem;
    width: 800px;
    height: 600px;
    padding: 50px;

}

Then I have a ajax call where in I am loading some html page like below.

JS:

$.post('/logme', function(resp) {
    $("#sentdiv").load("success.html");
});

here I want to unload the success.html after 5 sec and keep the with its CSS.

I have tried following

$("#sentdiv").delay(5000).replaceWith("<p>");
$("#sentdiv").delay(5000).fadeOut();

but It is moving the elements below it. I want to keep div and just remove the contents of div.

answered question

delay() is not a replacement for setTimeout(), as specifically noted in the API. delay() only affects the timing of animations that jQuery performs using it's internal animation queue.

1 Answer

8

use the jquery method .empty()

 $("#sentdiv").empty();

setTimeout(function() {
 $("#sentdiv").empty();
}, 5000)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sentdiv">

<span>this will be removed in five seconds...</span>

</div>

posted this

Have an answer?

JD

Please login first before posting an answer.