How-To's, HTML, JavaScript, Programming, Tech

How To Use One JavaScript Function to Slide Any HTML Element Up and Down with JQuery

In this guide I’ll show you how to use one JavaScript function to slide any HTML element up and down with JQuery by simply sending the JavaScript function the id of an element.

As I was creating a web application I realized that I had created multiple functions to handle the sliding up and down of different HTML elements. It’s very inefficient to have multiple functions or pieces of code that do the same thing. If you’ve taken programming classes you were probably told to use loops and method/functions when ever possible. Using methods and functions can save bandwidth and make web pages load faster because file sizes are smaller.

JavaScript function

Function slideUpDown takes in one parameter, which should be the id of the HTML element you want to slide up or down. It takes that id and checks whether it’s currently hidden. If the element is hidden it will slide the element down else it will slide it up to hide it.  Of course this function can be modified to use classes instead of ids, you would simply replace the “#” with “.” HTML

In this example I used a button to slide the div up and down, but you could use anything. The key here is the call to the JavaScript function with the id. The  div isn’t require around the paragraph in order to work, I just prefer to have elements surrounded by div’s.

If you have any questions feel free to leave a comment or email me at [email protected].

Share this Story
Load More Related Articles
Load More By Nick Escobedo
Load More In How-To's
  • Vani

    not working it is not sliding up and down

  • Amol

    Hi, do you know how to use slideUp up to SHOW elements instead of hide?

    • Can you explain a little more please? This function will show the element if it’s hidden and hide it if it’s show.

  • Ben

    If I wanted clicking a button to hide content under other buttons, how would I do that? For instance, if user clicks Button 1, Content 1 is displayed under it like normal. If user then clicks Button 2 with Content 1 still displayed, SlideDown Content 2 and SlideUp Content 1.

    • Ben please see my gist.
      Let me know if you have any further questions.

      • Ben

        I figured out another way to do it while I was waiting. I don’t know if it’s more elegant, but I’ll show you:

        I had another function:

        function slideUp(slider){

        $(“#” + slider).slideUp(‘slow’);


        And in each custom html, I slid up the id’s of all of the other button contents. To see it in action,

        • Ben

          Thanks for your quick response though!!

        • Yup that works too. Just two different approaches. I’m glad it worked out for you. You’re welcome happy to help!

Check Also

Selecting Your Next App Dependency

We often turn to open source or proprietary packages for adding functionality to our apps. Here are some key points and questions to ask when selecting your next dependency.