Slash Coding

  • Android Development
  • CSS
  • Subscribe
  • About Us
  • Contact Us
You are here: Home / Web Development / JavaScript / Create a Collapsible element using JavaScript

Create a Collapsible element using JavaScript

By Aneesh Bhatnagar

Sometimes hiding or showing some content to the user is to be decided. For instance, you are writing down the review of a movie and you don’t want the user to know the suspense by default. So, you can tell the user that this is a “Spoiler Alert”. If the user wants to read it, he or she can read it otherwise it will remain hidden to the user!

HTML Code ImageMaking such a thing is pretty easy but requires you to decide if you want the user to see the data by default or it must remain hidden by default. It totally depends on your application of this tip. Well, let’s get started writing down some basic HTML and CSS first. If you already don’t use Notepad++ for writing your codes, I recommend you to go ahead and get that at first. Read more about Notepad++ in my earlier article.

HTML and CSS for Collapsible Element

Start by creating a new document for this tutorial. Let’s get started.

  1. In the body tag of the webpage, create a new div tag element and give it the id as “expand“.
    <div id="expand">
    <!-- Content Goes here -->
    </div>
    
  2. Inside this div tag, place all the code that you want the user to see or not see.
  3. Now, in the head tag of the page, create a new style for the id expand by providing the following text in the head tag of the webpage.
    <style>
    div#expand{
    display:block;
    }
    </style>
    
  4. Now, in the body of the webpage, create a new anchor tag (a tag) which will control the element to be displayed or to be hidden. In this element, give the href value as “javascript:;” and the onclick value as “show()“.
    <a href="javascript:;" onclick=show()>Show/Hide Text</a>
    
  5. Well, we are now done with the HTML part of the page.
    Expanded View of the Element

Let’s now add the JavaScript code for this webpage to work properly.

JavaScript Code for Collapsible Element

The JavaScript code is pretty easy and clean to understand as well.

  1. In the head tag of the webpage, create  a script tag and create a new function called show in there.
    <script>
    function show()
    {
    //Function content goes here
    }
    </script>
    
  2. Inside this function, we need to check if the div element is currently hidden or shown. Do that by using an if statement and this line of code.
    if(document.getElementById('expand').style.display == 'none')
    
  3. Now, perform the action that you want and add the other code to the else part of the statement.
    document.getElementById('expand').style.display = 'block';
    else
    document.getElementById('expand').style.display = 'none';
    
  4. Well, that’s all. Just save the page and open in your browser to have a look at your newly created JavaScript effect.
    Collapsed View of the Element

This is how your HTML page should look like right now.

HTML Code view

And that’s all for this tutorial. I hope this tutorial helped you create something useful for yourself or your website. If you want, you can download the entire source code here.

Download Source Code

Keep subscribed to Slash Coding via RSS Feeds, Facebook, or Twitter. See you around! 😉

Did you enjoy this article?
Subscribe to our email alerts when we post something new so you don't miss out.

About Aneesh Bhatnagar

Aneesh Bhatnagar is a freelance web developer, who specializes in front-end website development with HTML5, CSS3 and jQuery. He believes in transforming the entire web into a Responsive Web. He also specializes in use of Bootstrap framework to get websites up and running.

Comments

  1. chris says

    July 13, 2015 at 11:48 pm

    HI, I would like to know how you would thus use this for multiple sections please? Thanks

    • Aneesh Bhatnagar says

      July 14, 2015 at 6:56 am

      Hey Chris,

      You can do it for multiple sections by changing the ID that we created in this tutorial (‘expand’) to something else for the second section and so on.. Similarly, it will require you to write a JavaScript code that will work on the newer element when you click on a new link.

  2. Bram Griffioen says

    March 20, 2017 at 1:51 am

    is it possible to first hide the text elements and after when on click it is shown?

    • Aneesh Bhatnagar says

      June 6, 2017 at 6:08 am

      Yes you can do that. Just set the initial value to be hidden and then it would work like that.

Trackbacks

  1. How to get link of collapsible div | ASP Questions & Answers says:
    July 1, 2014 at 10:27 am

    […] http://slashcoding.com/collapsible-element-using-javascript/ […]

  2. How to get link of collapsible div | ASP Web Form Data Control says:
    July 2, 2014 at 10:08 pm

    […] http://slashcoding.com/collapsible-element-using-javascript/ […]

  3. How to get link of collapsible div | ASP.NET MVC says:
    July 3, 2014 at 10:38 pm

    […] http://slashcoding.com/collapsible-element-using-javascript/ […]

Search Slash Coding

Follow Us

RSS Feeds Facebook Twitter Follow Google+

Categories

  • Android Development
  • C++
  • Developer Tips
  • Slash Coding
  • Web Development
    • CSS
    • JavaScript
    • jQuery
    • PHP

Recently Published

  • Moving Ahead from Front End and Android Development
  • How to Export a Project from Android Studio
  • What is jQuery? Let’s Answer this Common Question today!
  • Mobile App Prototyping : Pixate Studio
  • How to Create a Countdown Timer using jQuery

Subscribe to Email Alerts

Eager to learn more from Slash Coding?
Sign up for daily email updates when anything new comes up. Get it straight in your inbox.

Follow Us on Social Media

RSS Feeds Facebook Twitter Follow Google+

Copyright © 2025 · Slash Coding · Powered by the Genesis Framework
Sitemap | Privacy Policy | About | Contact

×
Get Notified of the new Tutorials when they are posted!
Never miss any article update from Slash Coding. Subscribe to email alerts today!