• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery fadeOut – Fading Out Element in HTML

jQuery fadeOut – Fading Out Element in HTML

February 27, 2022 Leave a Comment

We can use jQuery to fade out an element using the jQuery fadeOut() method. The jQuery fadeOut() method will change the opacity of an element to 0 and remove it from the HTML changing its CSS display property to “display: none”.

$("#div1").fadeOut(1000);

In the code above, the number “1000” is the number of milliseconds you want the fade out to happen by, which in this case is 1000 milliseconds or 1 second.

If you leave the fadeOut() method empty it will have the default time of 400 milliseconds.

$("#div1").fadeOut();

Let’s say we have the following HTML:

<div id="div1">
  <p>This is some div</p>
</div>

If we wanted to change the opacity of #div1 to 0 and hide it, we can use the jQuery fadeOut() method in the following jQuery code.

$("#div1").fadeOut();

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1").fadeOut();

Note that we could also do this by using the jQuery fadeTo() method. The main difference being that the fadeTo() method will change the opacity to 0, which means it won’t be visible but will still take up space. While the fadeOut() method will remove the div and it will not take up any space.

$("#div1").fadeTo(1000,0);

Using the jQuery fadeOut Method to Hide a Div with a Click

In this simple example, we will have a div with a greenish background. We will provide a button for the user to use to change the div’s opacity and hide it. We will also provide a button to show the div so that the user can fade in and fade out as many times as they want.

<style>#div2 { width: 300px; height: 200px; background: #7bbfa2; }</style>
<div id="div1">
  <div id="div2"></div>
  <div id="click-me1" class="click-me">Fade Out</div>
  <div id="click-me2" class="click-me">Fade In</div>
</div>

We can utilize both the jQuery click() method and jQuery fadeOut() method to hide the div.

We will also use the jQuery fadeIn() method to let the user show the div.

We will give both fadeIn() and fadeOut() methods the parameter 2000, which means it will display or hide the div in 2000 milliseconds(2 seconds).

Here is the JavaScript code below:

$("#click-me1").click(function(){
  $("#div2").fadeOut(2000);
});

$("#click-me2").click(function(){
  $("#div2").fadeIn(2000);
});

The final code and output for this example of using the jQuery fadeOut method to hide a div is below:

Code Output:

Fade Out
Fade In

Full Code:

<style>#div2 { width: 300px; height: 200px; background: #7bbfa2; }</style>
<div id="div1">
  <div id="div2"></div>
  <div id="click-me1" class="click-me">Fade Out</div>
  <div id="click-me2" class="click-me">Fade In</div>
</div>

<script>

$("#click-me1").click(function(){
  $("#div2").fadeOut(2000);
});

$("#click-me2").click(function(){
  $("#div2").fadeIn(2000);
});

</script>

Hopefully this article has been useful for you to understand how to use the jQuery fadeOut method.

Other Articles You'll Also Like:

  • 1.  jQuery Display None
  • 2.  Using jQuery to Replace a Class
  • 3.  jQuery last() – Get the Last Element
  • 4.  Using jQuery to Add a Sibling to an Element
  • 5.  Using jQuery to Get the Margin of an Element
  • 6.  Using jQuery to Get Value of Select On Change
  • 7.  jQuery get img src – Get the Source of an Image Using jQuery
  • 8.  Using jQuery to Move Element Before Another
  • 9.  Examples of Text Animations Using jQuery
  • 10.  Using jQuery to Check if Element Has Class

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy