The Herculoids
Herculoids A real groovy bunch of superheroes from Hanna-Barbera, circa 1967. For more about Gloop, Tara, and the rest of my old friends, click the link to check out Mr. Markstein's Toonopedia website.
Home Services Toolbox Downloads Gaming Tech News About Us

Open and Close DIV Layers



Invasive Procedures blog by brenz.net


Free News Headlines at RSSMall.com

Share this page: facebook myspace twitter bebo delicious digg stumbleupon buzz fark linkedin

Open/Close Div Layers

This code snippet demonstrates using a 'container' div, which can be expanded or collapsed to show/hide blocks of content.

For my javascript function names, I pondered items that are opened and closed. I considered eyes, cupboards, drawers, even silly sock hand puppets, but finally decided upon a lunchbox. If I were a betting man, I'd be willing to bet that everyone who sees this webpage has owned, or at least seen, a lunchbox. Purt' near 70 years have passed since I was a lunchbox-toting lad, but I vaguely recall my favorite lunchbox - it was a real dandy depicting a team of superheroes known as The Herculoids. I was particularly fond of Gloop and Gleep, those darn cute little blobby things. But I digress...

Pack the Lunchbox (HTML)

First, we'll create the HTML, consisting of a pair of <div> layers for each lunchbox. The first <div> layer (clasp_x) will use a hyperlink to trigger our javascript function that displays the contents of the second <div> (lunch_x). I've supplied the code for two lunchboxes below, but you can add as many more as you like, by incrementing the numeric suffix (x) of each pair of layer id's. Now you can add this HTML code to your page:
<div id="clasp_1" class="clasp"><a href="javascript:lunchboxOpen('1');">Open Lunchbox 1...</a></div>
<div id="lunch_1" class="lunchbox">This is lunchbox 1<br />Peanut Butter & Jelly<br />Potato Chips<br />Apple</div>
<div id="clasp_2" class="clasp"><a href="javascript:lunchboxOpen('2');">Open Lunchbox 2...</a></div>
<div id="lunch_2" class="lunchbox">This is lunchbox 2<br />Liverwurst (bleh!)<br />Twinkies<br />Fig Newtons</div>

Hide the Lunchbox (CSS)

Next, we'll create a couple of CSS classes, one of which will hold the hyperlink, and another whose contents are initially not displayed on the web page. Add these class definitions to your CSS stylesheet:
.clasp {
text-align:center;
}
.lunchbox {
display:none;
}

Open & Close the Lunchbox (javascript)

All that remains is to define two javascript functions - lunchboxOpen() and lunchboxClose() - which can be either in the <head> area of your HTML page, or in a separate javascript file:
function lunchboxOpen(lunchID) {
document.getElementById('lunch_' + lunchID).style.display = "block";
document.getElementById('clasp_' + lunchID).innerHTML="<a href=\"javascript:lunchboxClose('" + lunchID + "');\">Close Lunchbox " + lunchID + "...</a>";
}
function lunchboxClose(lunchID) {
document.getElementById('lunch_' + lunchID).style.display = "none";
document.getElementById('clasp_' + lunchID).innerHTML="<a href=\"javascript:lunchboxOpen('" + lunchID + "');\">Open Lunchbox " + lunchID + "...</a>";
}

Enjoy your Lunchbox!

Below are sample implementations of the lunchbox code. Depending on the CSS styles you apply to the clasp and lunchbox classes, you can achieve a variety of different looks.

Example 1

This is lunchbox 1
Peanut Butter & Jelly
Potato Chips
Apple
This is lunchbox 2
Liverwurst (bleh!)
Twinkies
Fig Newtons
This is lunchbox 3
Tuna Salad
Snack Pack Pudding
Carrots

Example 2

This is lunchbox 4
Peanut Butter & Jelly
Potato Chips
Apple
This is lunchbox 5
Liverwurst (bleh!)
Twinkies
Fig Newtons
This is lunchbox 6
Tuna Salad
Snack Pack Pudding
Carrots

Example 3

This is lunchbox 7
Peanut Butter & Jelly
Potato Chips
Apple
This is lunchbox 8
Liverwurst (bleh!)
Twinkies
Fig Newtons
This is lunchbox 9
Tuna Salad
Snack Pack Pudding
Carrots

Home Services Toolbox Downloads Gaming Tech News About Contact Us Privacy