So You Want To Toggle, Huh? Method One

By Joe Burns

http://www.htmlgoodies.com/beyond/dhtml/article.php/3470631/So-You-Want-To-Toggle-Huh-Method-One.htm (Back to article)

...use these to jump around or read it all
[Toggling with MSIE]
[Toggling in Netscape Navigator]
[Make the Division Appear]
[Make the Division Disappear]
[Call For the Disappear]
[More Divisions]

     First things first... This is a tutorial dealing with DHTML. You need to be running an Internet Explorer browser, 4.0 level or better, to see the effect.


Joe-ah Webster's Dictionary:

Toggle (tah' guhl) verb
     1. The ability to make items move
between being visible and being hidden.


Here's What I'm Talking About

     OK, I'm in that "naming-things" run around again. This tutorial is basically a DHTML session. What I'm going to show you here is how to make a division appear and disappear in MSIE. I also have a sister tutorial to this one that will teach you how to make a layer appear and disappear in Netscape Navigator. The effect is the same, but all the commands that do the trick are different so multiple companies have a hand in the process. Basically, this means that I'm about to get a bunch of letters that tell me that this effect is actually called "visibility", or "layering", or "Steve".

     The truth is, since the MSIE and Navigator browsers are moving in such different directions, it's hard to create one definitive statement that covers the effect. In one of my computer books, the author refers to making a layer appear and disappear as "toggling". I thought was as good a term as any.

     But no matter what you name it--the effect will still be as sweet. (That's Bill Shakespeare. I'm a cultured man you know.)


Toggling with MSIE

     In Microsoft Internet Explorer, you get the effect through DHTML commands. Now remember these commands are only supported in IE 4.0 and above, and are not supported in Navigator (as of 4/14/99). So when you set up this effect, make sure the users are running IE 4 or better. You can do that through setting up a browser detect script. If you don't then errors fly all over the place. This is a good one to make sure you people are prepared for.

But it's still really cool.

Dig This

     Oh, that's neat. And it's not that hard of an effect either. Basically what's happening is I have positioned a division on the page. In that division I put table cell with the words "How About This?" inside, but just about anything can be put in the division.


Make the Division Appear

     Once the division was created, encased it in a JavaScript function so I could call on it when ever I got a click or a mouse-over.
     Then I set up another JavaScript function so that when the mouse moved off of the link, the division would disappear again.
     Once I have a function set up that will make the division appear and make it disappear, then the process is simple...call on the correct function and the effect comes to life. Well...relatively simple anyway.

Make It Appear

     The harder of the two function is the one that makes the division appear, so we'll start with that one. It goes up in between the <HEAD> tags and it looks like this:


This button open a new window containing the code.
That way you can keep it open while I discuss it.


     That one purple line is pretty long, huh? Yeah. It can be broken down, if you really want, into multiple document.write statements, but why? It's just more typing for the same effect. I've got better things to do! OK, actually I don't, but I still hate writing things I don't have to.

     So, what does it do? Nothing. It won't do anything until it's called upon by its function name later in the page. Let's tear it apart:

     Now...take that function and stick it in between <SCRIPT LANGUAGE="javascript"> and </SCRIPT> commands and put that between the <HEAD> tags. So now, you understand and posses a function that will make the division appear. But can you make it disappear again? Here's how.


Make the Division Disappear

     What we need to do is set up another function.


If you haven't already, close the window with the "appear" function.
Open this one for the disappear function.


     This one's pretty easy to figure out even if DHTML is brand new to you. The function, named LoseIt() simply sets two sections of the division to represent nothing. In other words...it disappears.

     Remember...the name of the division is "TheTip". Go ahead and look at the appear function again if you missed that point. It's important. In this function we set two parameters, innerHTML and outerHTML, to nothing. Note the empty quote marks. The end. No more visible division. Very clever.

     Now, take that code and stick it between <SCRIPT LANGUAGE="javascript"> and </SCRIPT> commands and put that in between the <HEAD> tags.

      OK, now we're set. We can call for the division in the first function any darn time we feel like it.


Call For The Division

     Now that we have the two functions just waiting to be used, we can call for them as we would any other function. In the two examples shown in this tutorial, I set up a roll-over on a hypertext link and also made the division appear through the use of a form button. Here's the code for each.

The Hypertext Link:

<A HREF="http://www.htmlgoodies.com"
onMouseOver="ShowIt()" onMouseOut="LoseIt()">Hey Man!</A>

The Form Buttons

<FORM>
<INPUT TYPE="button" Value="let me see it" onClick="ShowIt()">
<INPUT TYPE="button" Value="OK, Take it Away" onClick="LoseIt()">
</FORM>

     There's no real science to it. I've called for the functions through basic onMouseOver and onMouseOut Event Handlers.


More Divisions

     This is a great effect if you have a series of links down one side of the page. The effect of multiple divisions appearing one after the other looks high-tech and appears interactive.

      The only downfall, if you want to call it that, is that each of these divisions are an element in their own right. They each have a NAME attribute assigned. Thus...you need to create a totally new function to make the division appear and disappear.

     For example, let's say you already have the division described in this tutorial installed on a page. You want a second one. Here's what you need to do:

     Now you're good to go with a second division. Yes, it's a little work, but the results are great.


A Final Note

     While working on this tutorial, I played with multiple and single divisions. I can honestly say that what makes these things really shine is the positioning element. Where they pop up is really the point of all of this, more so than the fact that they pop up at all. I found that you can't be overly precise, but get close. Or don't. I loved the look of an element on the left side of the page popping a window on the right.

     Got it? Great. Now go learn to toggle with Netscape's Layers.

 

Enjoy!

 

[Toggling with MSIE]
[Toggling in Netscape Navigator]
[Make the Division Appear]
[Make the Division Disappear]
[Call For the Disappear]
[More Divisions]