• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Hide and Display Content On Demand

By Will Bontrager | on May 13, 2005 | 0 Comment
JavaScript Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

You may have encountered web pages where an action on your part (like clicking a link or checking a checkbox or moving the mouse over a certain part of the web page) inserted causes additional content to appear on the page. The web site may have made it possible for another action to remove the content.

When the content appears, previously existing content is shifted around to make room for the new.

Images, iframes, forms, tables, text — all these visual elements can be inserted into and removed from web pages when the site visitor does predefined actions.

Copy ‘n paste code for a number of different predefined actions and types of content are linked from the http://willmaster.com/a/new/pl.pl?hidedisplay web page.

Examples I’ve found on the ‘net are horrifyingly complex. This article and the code at the above URL attempt to make it simple for anyone comfortable working with CSS and JavaScript. Those who are not yet so comfortable may find this article assists progress in that direction.

CSS is the key.

With CSS, a character, an image, a paragraph, everything within a DIV tag, or so forth, can be given what are called properties.

An example of a property is “font-size”. Properties can be given values.

An example of a “font-size” value is “24px” (meaning 24 pixels).

The way the above “font-size” property and “24px” value are specified in CSS is like this:

font-size: 24px;

Happily, CSS has a “display” property. And this property is used to insert or remove content within the page depending on pre-defined actions of site visitors.

Although the “display” property has many possible values, we will use only two for this project. These values are “none” and “” (the last is a null value, two quotation marks in succession).

The “display” property “none” value causes the content not to be displayed — or removed, if it’s already displayed. And the “” value causes the content to be displayed.

This is how the “none” value is specified in CSS:

display: none;

The “” value is specified by omitting the “display” property altogether.

Below are two examples in DIV tags.

The first will display. The second will not.

<div id="abc" style="font-size: 24px;">

Hello!
</div>
<div id="xyz" style="font-size: 24px; display: none;">
Good-bye!
</div>

Give the above a try. Pop the HTML into an example web page and load it into your browser. You’ll see “Hello!” but you won’t see “Good-bye!”

The ID attributes in the DIV tags are there so JavaScript can change the “display” property.

JavaScript acts as switch.

JavaScript is used to display and remove content by changing the value of the CSS “display” property. Below is an example JavaScript. (I’ll show you how to run the JavaScript in a moment.)

<script type="text/javascript" language="JavaScript"><!--

function RemoveContent(d) {
document.getElementById(d).style.display = "none";
}
function InsertContent(d) {
document.getElementById(d).style.display = "";
}
//--></script>

Put the JavaScript on the example web page with the two example DIV tags. The JavaScript can be in the HEAD or BODY area of the web page, above or below or between the DIV tags — whatever location makes sense to you.

When the JavaScript is called, the DIV to remove or the DIV to display are specified in the link. Here are the HTML links to do it:

<a href="javascript:RemoveContent('abc')">

Remove Hello!
</a><br>
<a href="javascript:InsertContent('xyz')">
Insert Good-bye!
</a><br>
<a href="javascript:RemoveContent('xyz')">
Remove Good-bye!
</a><br>
<a href="javascript:InsertContent('abc')">
Insert Hello!
</a>

Put the links somewhere on your example web page. (If you put them below the example DIV tags, you’ll see how the link text is pushed down to make room when content is inserted above it.)

Play with it. You’ll quickly gain an idea of how it works.

For many more examples, along with copy ‘n paste code, see the http://willmaster.com/a/new/pl.pl?hidedisplay web page.

If you know exactly what you want to accomplish, it might be there, or something close enough that some simple modification can perfect it.

If you’re just looking, the examples may provide the idea that will enhance your web site.

Share this story:
  • tweet

Author Description

Publication: WillMaster Possibilities

No Responses to “Hide and Display Content On Demand”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,242Followers 492Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.