• 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 slideUp – How to Slide Up and Hide an Element Using jQuery

jQuery slideUp – How to Slide Up and Hide an Element Using jQuery

March 4, 2022 Leave a Comment

We can use jQuery slideUp() method to hide a div in HTML. The jQuery slideUp() method will slide the element out of view and then hide it.

$('#div1').slideUp();

You can also enter milliseconds as a parameter in the slideUp() method to change the speed at which the slide up happens.

$('#div1').slideUp(1000);

The default speed is 400 milliseconds. In the example above, the div would slide up and out of view at 1000 milliseconds, or 1 second.

Let’s take a look at a very simple example below.


Let’s say we have the following HTML:

<div id="div1">
  <div id="div2"></div>
  <div id="div3"></div>
</div>

To hide #div3 we could use the jQuery slideUp() method

$('#div3').slideUp();

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

jQuery('#div3').slideUp();

Let’s take a look at another example below.

Using the jQuery slideUp Method to Start a Simple Navigation Bar

In this example, we will build the start to a nav bar. The navigation menu will work by hovering over a menu item, and then showing new menu items below the div you hovered over.

Here is the HTML and CSS setup:

<style>.example-menu{ height: 220px; } 
.menu-item-container { position: relative; }
#menu-item1 { position: relative; display: inline-block; } 
.menu-items { display: block; padding: 10px 14px; border: 1px solid #444; background: #fff; text-decoration: none; } 
.menu-sub-items { position: absolute; display: none; }
</style>
<div class="example-menu">
  <div class="menu-item-container">
    <div id="menu-item1" class="menu-items">About</div>
    <div id="menu-sub-item1" class="menu-sub-items">
      <a class="menu-items" href="#">Who We Are</a><a class="menu-items" href="#">Staff</a><a class="menu-items" href="#">Contact Us</a>
    </div>
  </div>
</div>

In our JavaScript code, we will make use of the jQuery hover() method first. When the user hovers over the “About” menu item, we will then use the jQuery slideDown() method to show the sub menu that was hidden.

The final part to this example will be to hide the sub-menu again when the user’s mouse leaves the menu. We will use the jQuery mouseleave() method to do this.

Once we leave the sub-menu, we will use the jQuery slideUp() method to hide the sub menu.

Here is the JavaScript code:

$("#menu-item1").hover(function(){
  $("#menu-sub-item2").hide();
  $("#menu-sub-item1").slideDown();
});

$(".menu-item-container").mouseleave(function(){
  $("#menu-sub-item1").slideUp(100);
});

The final code and output for this example of using the jQuery slideUp method to start a simple navigation bar is below:

Code Output:

About
Who We AreStaffContact Us

Full Code:

<style>.example-menu{ height: 220px; } 
.menu-item-container { position: relative; }
#menu-item1 { position: relative; display: inline-block; } 
.menu-items { display: block; padding: 10px 14px; border: 1px solid #444; background: #fff; text-decoration: none; } 
.menu-sub-items { position: absolute; display: none; }
</style>
<div class="example-menu">
  <div class="menu-item-container">
    <div id="menu-item1" class="menu-items">About</div>
    <div id="menu-sub-item1" class="menu-sub-items">
      <a class="menu-items" href="#">Who We Are</a><a class="menu-items" href="#">Staff</a><a class="menu-items" href="#">Contact Us</a>
    </div>
  </div>
</div>

<script>

$("#menu-item1").hover(function(){
  $("#menu-sub-item2").hide();
  $("#menu-sub-item1").slideDown();
});

$(".menu-item-container").mouseleave(function(){
  $("#menu-sub-item1").slideUp(100);
});

</script>

Hopefully this article has been useful for you to understand how the jQuery slideUp() method works.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Append Text to textarea Field
  • 2.  Toggle the Visibility of an Element Using jQuery
  • 3.  jQuery hasClass – How to Check if an Element Has a Certain Class
  • 4.  jQuery keydown Method
  • 5.  Using jQuery to Change Div Text
  • 6.  Resizing an Image Using jQuery
  • 7.  jQuery siblings – Get the Sibling Elements of a Div
  • 8.  Using jQuery to Select Multiple Ids
  • 9.  Using jQuery to Change the Id of a Div
  • 10.  Using jQuery to Get Text Length

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