It's Sean!

UK freelance journalist, author
and writer Sean McManus

Printed from © Sean McManus.
Home > Articles > Webmaster resources > Javascript examples > Javascript hide / reveal script

Using Javascript to hide and reveal text and graphics

One of the most useful things you can do with javascript and CSS is to make things appear and disappear when you want to. It means you can focus the user's attention on what's needed at a particular stage of a process (eg e-commerce), or you can create special effects by making text or graphics pop onto the screen instantly.

I created a tool that enables people to get their copy of my novel University of Death signed over the internet. This uses the script to good effect to make the 'signed' letter instantly appear.

There are lots of javascript and CSS libraries that have features built in to hide and reveal different sections of the page, but they usually carry a heavy download penalty. Unless you wanted to use the library for something else (and something much more difficult, at that), then you're better off adapting this short free script to hide and reveal webpage content.

Shall we try an example?

I've got hiccups. Give me a surprise!

How it works

This is easy. Here are three steps to getting it working:

  1. Create the content you want to hide. Put it inside a DIV and give it an ID. Let's call our DIV 'hideaway'. If you have more than one on a page, give them different names. You also need to set the style to "display:none;" if you want it to start off hidden. It should look like this:
    <div id="hideaway" style="display:none;">Here's the hidden content</div>
  2. Create your link and put in the following Javascript:
    <a href="javascript:;" onClick="document.getElementById('hideaway').style.display='block';">Reveal hidden content</a>
  3. To create a link that hides it again, using the following Javascript:
    <a href="javascript:;" onClick="document.getElementById('hideaway').style.display='none';">Hide content again</a>

The content that you make appear and disappear can include image tags and any other HTML - it's just text in the above to make it easier to understand.

More advanced stuff you can do

That technique is fairly basic, but it underlines some quite sophisticated effects. Here are some ideas for other ways to use it:

Books by Sean McManus

Scratch Programming in Easy Steps

Scratch Programming in Easy Steps

Raspberry Pi For Dummies

Raspberry Pi For Dummies

Learn to program with the Scratch programming language, widely used in schools and colleges.

Set up your Pi, master Linux, learn Scratch and Python, and create your own electronics projects.

Coder Academy

Coder Academy

Cool Scratch Projects in Easy Steps

Cool Scratch Projects in Easy Steps

Learn to make games and other programs in Scratch 2.0, and make a web page in HTML, with this highly interactive book for 7-10 year olds.

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

More books

Recommended articles

Stylised coloured photo of a synth

Discover my music and home recording articles

Interested in starting home recording? Follow me as I start recording, using instruments such as the Novation MiniNova and Korg Kaossilator, and share my synth and sample-based music online.

Scratch cat and rainbow circle

10-block demos for Scratch programming

Lots of people of all ages are learning to program with Scratch. Discover some useful tricks and techniques in my 10-block Scratch demos, including special effects you can drop into your games.

Paper Raspberry Pi case

Download free book chapters and more!

Download a free case for your Raspberry Pi, and free chapters from my books about the Raspberry Pi, Scratch, web design and more!

©Sean McManus.