Hide/reveal DHTML

A

ATS

I need to hide/reveal parts of a web page using javascript. I think I can
do with using the <span> tag, but I've been away from it for a while and
don't remember. Any pointers, examples?
 
M

Michael Winter

I need to hide/reveal parts of a web page using javascript. I think I
can do with using the <span> tag, but I've been away from it for a while
and don't remember. Any pointers, examples?

This has been asked *many* times. Please look through the group archives:

<URL:http://groups.google.com/groups?q=g...ipt+show+hide+element+|+div+|+layer&scoring=d>

Though the posts you find will apply mainly to DIVs and other block-level
elements, the principle can be applied to any element.

The only comment I would like to add that may not be present in all of the
threads is that content should not be hidden initially with CSS. If the
script cannot execute (for whatever reason), the user will not be able to
view the content. Instead, hide the appropriate content with the script
when the document loads.

Good luck,
Mike
 
W

WindAndWaves

ATS said:
I need to hide/reveal parts of a web page using javascript. I think I can
do with using the <span> tag, but I've been away from it for a while and
don't remember. Any pointers, examples?
Here is a recent example that I made with help from the group

www.sunnysideup.co.nz/clients/friars/listing.html

Note that the <DIV ID=bas> is the crucial Div, any divs with IDs within the
Bas (stands for base) div will be hidden and the <A HREFs> in the menu allow
the user to see them.

- Nicolaas
 
S

SimonFx

Here is a recent example that I made with help from the group

www.sunnysideup.co.nz/clients/friars/listing.html

Note that the <DIV ID=bas> is the crucial Div, any divs with IDs within the
Bas (stands for base) div will be hidden and the <A HREFs> in the menu allow
the user to see them.

Except DIVs nested within DIVs nested in BAS are not hidden by
themselves, but only when the parent DIV is hidden. Hmmm.

<DIV ID=BAS>
<DIV ID=D1>D1</DIV>
<DIV ID=D2>D2</DIV>
<DIV ID=D3>D3 <DIV ID=D4>D4</DIV> </DIV>
</DIV>

D4 does not have its style set with "display:none". D3 can/does so D4 is
dependent on D3. But what I am trying to say is that the function won't
work on children of child DIVs (grandchild DIVs?) of BAS (ie: D4).
 
I

Ivo

that content should not be hidden initially with CSS. If the
script cannot execute (for whatever reason), the user will not be able
to view the content. Instead, hide the appropriate content with the
script when the document loads.

This is a difficult dilemma. Your point is very valid, we should always be
conscious of javascript not being present or enabled, but if there is one
thing that makes a site look unprofessional in my view, it is pages that
fall apart and reassemble themselves after loading, especially when hidden
content is involved. The average computer user is feeling uncertain enough
already.
I always try to design so that a page stands solid the moment it is
requested, and do any manipulations *during* load. If the user has no
javascript, he will be missing out on a lot of fun anyway.
 
M

Michael Winter

[...] content should not be hidden initially with CSS.
[snip]

This is a difficult dilemma. Your point is very valid, we should always
be conscious of javascript not being present or enabled,

Particularly as here in the UK, service-providing sites need to be
accessible.
but if there is one thing that makes a site look unprofessional in my
view, it is pages that fall apart and reassemble themselves after
loading,

That is true.
especially when hidden content is involved. The average computer user is
feeling uncertain enough already.

I suppose when you should hide content depends on what, and how much, you
are hiding, and what impact that would have on the page. If the impact is
high, it might be best to hide as soon as possible. However, if the impact
is low, particularly if the content is below the fold, then you might get
away with hiding everything with a SCRIPT element at the end of the BODY
element, meaning you don't have to litter the source with SCRIPT elements.
I always try to design so that a page stands solid the moment it is
requested, and do any manipulations *during* load.

I didn't mean to say that you *must* wait until the page and all it's
content has loaded. The main point was to make the page accessible. The
best way to achieve that will, I suspect, *have* to be decided by the
author for his specific circumstance.
If the user has no javascript, he will be missing out on a lot of fun
anyway.

Quite possibly, but I don't need to tell you that content is king. Well,
usually. :)

Mike
 
A

ATS

Very sorry about the late reply.

I looked over the discussions and found it mind numbing. At my age most
things are, but I work with what I have. Perhaps if I expanded on what I'm
after, you could point me in the right direction.

I have a page (http://www.democratsforum.com) that displays various text
based on the day of the week. That way I can load it one day and leave it
for the week. I have two other pages that do the same type of thing
(displaying phototoons). I'm currently showing or hiding a text area and
the problem is there is a large space at the top of the page that is empty.

It's been a few years since I wrote web pages, but there was a fundamental
incompatibility between Netscape and IE. IIRC, "Div" is an IE creature and
"Layers" is the Netscape equivalent. I wanted this to avoid complications
(thus using the more universal "Span" in case someone else has to maintain
it as I might be leaving the area for up to two years.

I was thinking of having the existing code reveal/hide areas (span? div?
layers?) that would include the text that also includes HTML code and be
positions so it was at the top of the page.

(BTW, is my Outlook goofy or is it normal that this message keep
disappearing behind the message window?)
 
M

Michael Winter

On Thu, 2 Dec 2004 12:10:05 -0600, ATS

[snip]

You should seriously consider:

1) Dropping frames. They're evil
(<URL:http://www.google.com/search?q="frames+*+evil">).
2) Validating your HTML ( said:
I'm currently showing or hiding a text area and the problem is there is
a large space at the top of the page that is empty.

Use "display: none" not "visibility: hidden".

[snip]
IIRC, "Div" is an IE creature

No. DIVs are standard HTML elements and have been since HTML 3.2[1] (1997).
and "Layers" is the Netscape equivalent.

The LAYER element was only supported in NN4, I believe. Certainly, no
versions since then have used it.
I wanted this to avoid complications (thus using the more universal
"Span" [...]

The SPAN element is completely different from both of these. For a start,
it's an inline element, whilst the other two are block-level. See
I was thinking of having the existing code reveal/hide areas (span? div?
layers?) that would include the text that also includes HTML code and be
positions so it was at the top of the page.

As you seem to have server-side support, why not use it to include the
text and remove your reliance upon client-side scripting?
(BTW, is my Outlook goofy or is it normal that this message keep
disappearing behind the message window?)

I wouldn't know. I don't use Outlook.

Mike


[1] A DIV element is also used in the HTML 2.0 Specification (1995), but
it's not present in the DTD. I'm not quite sure what's going on there.

We're now on HTML 4.01 (1999), by the way.
 
A

ATS

Thank you. I appreciate the time. I'll look at the "Div" stuff again now
that I know it works on pretty much everything. Once I figure out a way to
do a menu system, I've been considering moving to the non-frame environment.
Again with the idea of simplifying the site. That makes going to the
validator easier.

Not sure what you mean by server v. client side includes. I assume server
side is better because the client doesn't have to work for it. Thus I would
use include statement to external files on the server?

Lee
 
M

Michael Winter

I didn't say anything about server-side includes. :p

[snip]
You still need the JS (as Mike noted) to *hide* the content when it is
not needed for the page.

Actually, my suggestion was to avoid that altogether. With proper
server-side support, you can enter the text into a database[1]. The server
can then find today's text and write that in. No client-side scripting
required at all, and much smaller files sent to the visitor.

How you do it exactly, could depend on the amount of traffic you receive.
If it's high, it would probably be worthwhile having the server
automatically generate a static file at the beginning of each day and
serve that, rather than constructing it on each request.

[snip]

Mike


[1] Not necessarily an *actual* database. It might be easier to write a
series of plain text files that would be inserted verbatim into the page,
named with the date of their intended inclusion.
 
R

Robert

ATS said:
Please see my response to Mr. Winter. What's in the javascript file?

Here is an HTML file which shows two ways of hiding text.

Robert


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>CSS Display and Visibility</title>

<script type="text/javascript">

function validate(x)
{
var checkedButton;

// Figure out which radio button was pressed

checkedButton = findValue(x.receiveVia);

var varName = x.theName.value;
var varEmail = x.theEmail.value;
var varAddress = x.theAddress.value;

// I changed submitOK to a boolean variable.
var submitOK = true;

// Validate email: name and email

if (checkedButton == "byEmail")
{

if (varName == '')
{
alert("Please fill in your Name");
submitOK = false;
}
if (varEmail == '')
{
alert("Please fill in Email");
submitOK = false;
}
if (varAddress != '')
{
alert("Please erase the address field.");
submitOK = false;
}

return submitOK;

}

// Validate print: name, email, and address

else if (checkedButton=="printed")
{
// Error messages should be in the order on the form
if (varName.length == '')
{
alert("Please fill in your Name");
submitOK = false;
}
if (varEmail == '')
{
alert("Please fill in Email");
submitOK = false;
}
if (varAddress == '')
{
alert("You must enter your Address");
submitOK = false;
}

return submitOK;
}
else
{
alert("You need to select either email or print.");
return false;
}
}

function vanishHidden(doWhat)
{
// Check if the getElementById method is available
if (document.getElementById)
{
document.getElementById("hideSpan").style.display = doWhat;
}
else if (document.all)
{
alert("Running an older version of IE.");
document.all.hideSpan.style.display = doWhat;
}
else
{ alert("Cannot change visibility of address field"); }
}


function hideHidden(doWhat)
{
// Check if the getElementById method is available
if (document.getElementById)
{
document.getElementById("vanishSpan").style.visibility = doWhat;
}
else if (document.all)
{
alert("Running an older version of IE.");
document.all.vanishSpan.style.visibility = doWhat;
}
else
{ alert("Cannot change display value of address field"); }
}

// See which radio button is selected and return its value
function findValue(obj)
{
var i, theValue;
theValue = null;

for (i=0;i<obj.length;i++)
{
if (obj.checked == true)
{
theValue = obj.value;
break;
}
}

return theValue;
}
</script>

</head>

<body>

<p>Please try out our form.</P>
<P>This form uses the CSS display
and visibility style attributes. When you click on the
radio button email, Javascript code uses the display attribute
property of hidden to exclude the address field from the display.
No space will be taken up in the window.
When you click on the no radio button, Javascript code uses the
visibility attribute property of none to make the literature
catagories invisible. Space will be taken up in the window.</p>

<form name="myForm"
action="http://www.notavalidurl.com"
method="GET"
onsubmit="return validate(document.forms['myForm']);">
<p>
<input type="radio"
name="receiveVia"
value="printed"
onclick="vanishHidden('');">
Printed brochure</p>
<p>
<input type="radio"
name="receiveVia"
value="byEmail"
onclick="vanishHidden('none');
document.forms['myForm'].theAddress.value = '';">
Via Email</p>
<p>Name:<br>
<input type="text"
name="theName"
size="20"><br><br>
Email:<br>
<input type="text" name="theEmail" size="20">
<br><br>
<span id="hideSpan">
Postal address:<br>
<input type="text" name="theAddress" size="40">
</span>
</p>
<p>
Do you wish to receive additional literature?
<br>
<input type="radio"
name="literature"
value="yes"
onclick="hideHidden('visible')";>&nbsp;Yes&nbsp;&nbsp;
<!-- use visibility. -->
<span id="vanishSpan">
<input type="checkbox"
name="gardening"
value"gardening">&nbsp;Gardening
<input type="checkbox"
name="kitchen"
value"kitchen">&nbsp;Kitchen
<input type="checkbox"
name="vacation"
value"vacation">&nbsp;Vacation
<!-- Just get it done. I know there are better ways. -->
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox"
name="office"
value"office">&nbsp;Office
<input type="checkbox"
name="secondhome"
value"secondhome">&nbsp;Second Home
</span>

<br>
<input type="radio"
name="literature"
value="no"
onclick="hideHidden('hidden');
var d=document.forms['myForm'];
d.elements['gardening'].checked=false;
d.elements['kitchen'].checked=false;
d.elements['vacation'].checked=false;
d.elements['office'].checked=false;
d.elements['secondhome'].checked=false;">
No
</p>
<p><input type="submit"
border="0"
value="Submit form"
width="75"
height="17"
ALT="Submit button"></p>

</form>

<script type="text/javascript">
// In the case of a reload, the radio button may already be clicked.
// This code needs to be after the form.
var x = document.forms["myForm"];
if (x.receiveVia[0].checked == true)
{ vanishHidden('');}
else if (x.receiveVia[1].checked == true)
{ vanishHidden('none');}
else
{ ;}

if (x.literature[0].checked == true)
{ hideHidden('visible');}
else if (x.literature[1].checked == true)
{ hideHidden('hidden');}
else
{ ;}

</script>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top