Nikhil Kulkarni
Nikhil Kulkarni

Reputation: 674

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

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 div 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.

Upvotes: 0

Views: 144

Answers (1)

zfrisch
zfrisch

Reputation: 8670

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>

Upvotes: 1

Related Questions