Yogesh Chauhan's Blog

How to Hide HTML elements automatically after few seconds using JavaScript?

in HTML & JavaScript on April 19, 2020

Just add the HTML element. Div or any other element.

HTML


<div id="hide_the_div">
  This div will hide automatically after 10 seconds
</div>

Add some CSS to make it visible. You can just purely use text for this example. No need to use CSS just to see how it works.

CSS


// just to add some style

#hide_the_div{
  width:200px; 
  color:green; 
  padding:2%; 
  text-align:center;
}

Lastly, add a jQuery function calling the method fadeOut on div element using the ID of the element. 10000 = 10 seconds so you can adjust the time according to your need.

JavaScript


$(function() {
  setTimeout(function() { $("#hide_the_div").fadeOut(2000); }, 10000)
})

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to create a Recent Posts function in WordPress?WordPressHow to add a Bar Chart in Angular App?AngularHow to create rotating texts using JavaScript and CSS?CSSSolution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)MiscellaneousHow to render Lists in React?ReactHow to get recent posts in WordPress?WordPress3 JavaScript methods to use when you want to go back and forth in historyJavaScript12 URLSearchParams methods in JavaScriptJavaScriptRecursion in JavaScriptJavaScriptCheck if any column has NULL values in PostgresPostgresDISTINCT ON: The confusing, unique and useful feature in PostgresPostgresINTERSECT and EXCEPT in PostgresPostgres