Categories
JavaScript Answers

How to Hide a Div After a Few Seconds with JavaScript and jQuery?

Spread the love

We can use the jQuery fadeOut method to hide a div.

And we can combine that with setTimeout to delay the execution of fadeOut .

If we have the following HTML:

<div id='mydiv'>  
  hello world  
</div>

Then we can call fadeOut by writing:

setTimeout(() => {  
  $('#mydiv').fadeOut('fast');  
}, 1000);

We call fadeOut in the setTimeout callback with a delay of 1000 milliseconds.

And now we should see the div fade away after 1 second.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *