472,789 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

Hide / Show form Elements.

I've found this script that allows be to hide/show form elements..

<script language="JavaScript"><!--
var toggle = true;

function show(object) {
if (document.layers && document.layers[object])
document.layers[object].visibility = 'visible';
else if (document.all) {
document.all[object].style.visibility = 'visible';
document.all[object].style.zIndex = 100;
}
}
function hide(object) {
if (document.layers && document.layers[object])
document.layers[object].visibility = 'hidden';
else if (document.all)
document.all[object].style.visibility = 'hidden';
}
//--></script>

<form>
<input type="button" onClick="if (toggle = !toggle) hide('myId');else
show('myId')" value="Show/Hide">
</form>

<div id="myId" style="position: absolute; visibility:
visible;">Test</div>
This works well, but I have a couple questions...

1. Is there anyway to default this to HIDE ?
2. How would I change the button, to a checkbox or image ?
3. Any other script that will do that ?

Thanks

Sep 8 '05 #1
4 7905
je***********@yahoo.com wrote:
I've found this script that allows be to hide/show form elements..

<script language="JavaScript"><!--
The language attribute is depreciated, type is required. Hiding scripts
is no longer necessary.

<script type="text/javascript">
var toggle = true;

function show(object) {
if (document.layers && document.layers[object])
document.layers[object].visibility = 'visible';
else if (document.all) {
document.all[object].style.visibility = 'visible';
document.all[object].style.zIndex = 100;
It will not work in browsers that don't support either document.all or
layers (which is quite a few).
}
}
function hide(object) {
if (document.layers && document.layers[object])
document.layers[object].visibility = 'hidden';
else if (document.all)
document.all[object].style.visibility = 'hidden';
}
//--></script>

<form>
<input type="button" onClick="if (toggle = !toggle) hide('myId');else
show('myId')" value="Show/Hide">
</form>

<div id="myId" style="position: absolute; visibility:
visible;">Test</div>
This works well, but I have a couple questions...
Then you didn't test it very extensively - it is also very clumsy.

Try:

function showHide( id )
{
var el;
if (document.getElementById) {
el = document.getElementById(id);
} else if (document.all) {
el = document.all[id];
} else if (document.layers) {
el = document.layers( id )
}

if ( el.style ){
el.style.visibility =
('visible' == el.style.visibility)? 'hidden':'visible';
}
}

The button becomes:

<input type="button" onclick="showHide('myId');" value="Show/Hide">

And the div:

<div id="myId" style="visibility: visible;">Here is my div</div>
I'd consider dropping the document.layers bit unless you have enough
Netscape 4 visitors to justify it.

1. Is there anyway to default this to HIDE ?
Use the above function and in the body tag:

<body onload="showHide('myId');">

That way users without JavaScript will not have the element hidden
(without JS they'll never be able to reveal it).
2. How would I change the button, to a checkbox or image ?
First, you don't need a form around your button if the button is just
there by itself.

Second, you can use any element that has an onclick attribute (or any
other intrinsic event you choose to use) defined in the HTML spec (which
is most of them). But if you use a checkbox, should the element be
hidden when its selected or shown?

Let's say you want it hidden:

<input type="checkbox" onclick="showHide2(this, 'myId');">Hide the div

<script type="text/javascript">
function showHide2(cb, id)
{
if (!document.getElementById || !document.body.style) return;
var el = document.getElementById( id );
el.style.visibility = (cb.checked)? 'hidden':'visible';
}
</script>
3. Any other script that will do that ?


See above.

--
Rob
Sep 8 '05 #2
RobG.. Thanks I will try this..

The script I posted, I found on a script site.. It's not one I wrote !!

Thanks again !!
RobG wrote:
je***********@yahoo.com wrote:
I've found this script that allows be to hide/show form elements..

<script language="JavaScript"><!--


The language attribute is depreciated, type is required. Hiding scripts
is no longer necessary.

<script type="text/javascript">
var toggle = true;

function show(object) {
if (document.layers && document.layers[object])
document.layers[object].visibility = 'visible';
else if (document.all) {
document.all[object].style.visibility = 'visible';
document.all[object].style.zIndex = 100;


It will not work in browsers that don't support either document.all or
layers (which is quite a few).
}
}
function hide(object) {
if (document.layers && document.layers[object])
document.layers[object].visibility = 'hidden';
else if (document.all)
document.all[object].style.visibility = 'hidden';
}
//--></script>

<form>
<input type="button" onClick="if (toggle = !toggle) hide('myId');else
show('myId')" value="Show/Hide">
</form>

<div id="myId" style="position: absolute; visibility:
visible;">Test</div>
This works well, but I have a couple questions...


Then you didn't test it very extensively - it is also very clumsy.

Try:

function showHide( id )
{
var el;
if (document.getElementById) {
el = document.getElementById(id);
} else if (document.all) {
el = document.all[id];
} else if (document.layers) {
el = document.layers( id )
}

if ( el.style ){
el.style.visibility =
('visible' == el.style.visibility)? 'hidden':'visible';
}
}

The button becomes:

<input type="button" onclick="showHide('myId');" value="Show/Hide">

And the div:

<div id="myId" style="visibility: visible;">Here is my div</div>
I'd consider dropping the document.layers bit unless you have enough
Netscape 4 visitors to justify it.

1. Is there anyway to default this to HIDE ?


Use the above function and in the body tag:

<body onload="showHide('myId');">

That way users without JavaScript will not have the element hidden
(without JS they'll never be able to reveal it).
2. How would I change the button, to a checkbox or image ?


First, you don't need a form around your button if the button is just
there by itself.

Second, you can use any element that has an onclick attribute (or any
other intrinsic event you choose to use) defined in the HTML spec (which
is most of them). But if you use a checkbox, should the element be
hidden when its selected or shown?

Let's say you want it hidden:

<input type="checkbox" onclick="showHide2(this, 'myId');">Hide the div

<script type="text/javascript">
function showHide2(cb, id)
{
if (!document.getElementById || !document.body.style) return;
var el = document.getElementById( id );
el.style.visibility = (cb.checked)? 'hidden':'visible';
}
</script>
3. Any other script that will do that ?


See above.

--
Rob


Sep 8 '05 #3
<input type="checkbox" onclick="showHide2(this, 'myId');">Hide the div

<script type="text/javascript">
function showHide2(cb, id)
{
if (!document.getElementById || !document.body.style) return;
var el = document.getElementById( id );
el.style.visibility = (cb.checked)? 'hidden':'visible';
}
</script>

I like this idea.. But I would prefer it hidden by default, and shown
when selected.

How do I do that ?? THANKS

Sep 8 '05 #4
je***********@yahoo.com wrote:
<input type="checkbox" onclick="showHide2(this, 'myId');">Hide the div

<script type="text/javascript">
function showHide2(cb, id)
{
if (!document.getElementById || !document.body.style) return;
var el = document.getElementById( id );
el.style.visibility = (cb.checked)? 'hidden':'visible';
}
</script>

I like this idea.. But I would prefer it hidden by default, and shown
when selected.

How do I do that ?? THANKS


Make your div not visible:

<div ... visibility: hidden;">Test</div>
Change the text on the checkbox:

<input type="checkbox" onclick="showHide2(this, 'myId');">Show the div
Adjust the logic of the function:

...
el.style.visibility = (cb.checked)? 'visible':'hidden';
And now users with JS disabled or not available will never see the
content - I guess they could look at the source. :-x

This issue is normally solved by making your page function properly
without any scripting at all, then add script to enhance the 'user
experience'. It is not good to have non-functioning checkboxes and
buttons, so often other UI elements are used.

The example below demonstrates some of this, it runs an onload function
to add the onclick and hide stuff. If the page is big and heavy, the
initialisation script could be added after the last 'container' div so
it runs before the page has fully loaded. Otherwise users may see all
the content displayed, then see it disappear when the onload runs.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Show/hide play</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<style type="text/css">
.heading {
font-weight: bold; text-decoration: underline;
cursor: pointer; font-family: verdana, sans-serif;
padding-top: 5px; display: block;
}
.content {background-color: #dddddd; padding-left:10px;}
</style>
<script type="text/javascript">
function showHide()
{
var idx = this.id.split('-')[1];
var sp = document.getElementById('cont-'+idx);
sp.style.display = ('' == sp.style.display)? 'none':'';
}

function initContent()
{
// Check that necessary features are supported
if ( !document.getElementById
|| !document.getElementsByTagName
|| !document.body.style){
return;
}
// Get all the div elements we need
var x = document.getElementById('container');
var divs = x.getElementsByTagName('div');
// Depending on classname, add an onclick or hide
var el, i = divs.length;
while ( i-- ){
el = divs[i];
if (el.className && /\bheading\b/.test(el.className)){
el.onclick = showHide;
} else if (el.className && /\bcontent\b/.test(el.className)){
el.style.display = 'none';
}
}
}
window.onload = initContent;
</script>
</head><body>
<div style="border: 1px solid #ddddff;" id="container">
<div class="heading" id="head-1">
Here is the first heading</div>
<div class="content" id="cont-1">
Here is the first content<br>
Here is the first content<br></div>
<div class="heading" id="head-2">
Here is the second heading</div>
<div class="content" id="cont-2">
Here is the second content<br></div>
</div>
</body>
</html>

--
Rob
Sep 9 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Wang, Jay | last post by:
I try to group several rows in a table into a div and show/hide them by click on a button somewhere with a javascript link. When clicked, the link will toggle the style of the div section's style...
19
by: benzwt | last post by:
I use the following function to hide a <div> named one. function hideObject() { if (ns4) { document.n1.visibility = "hide"; } else if (ie4) { document.all.style.visibility = "hidden"; }...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
2
by: MOHSEN KASHANI | last post by:
Hi, I am trying to hide some form elements in a form by default and show/hide depending on which radio button is clicked. This is what I have but it is not working: <head> <style> ..noshow {...
1
by: asilverpeach | last post by:
Hey Guys! Found some great scripts here on this topic but have to make to changes to the code that I can't seem to figure out. First, In the following code clicking on the headers shows the...
13
by: jeff | last post by:
I am attempting to be able to show and hide a form called viewAllForm. I declared an instance of the form in a module. Public viewAllForm As New frmViewAll However I keep getting runtime errors...
3
by: toodi4 | last post by:
I'm using a javascript that hides and unhides text based on a button click. It works great across static fields on a form. The problem I have is that I'm trying to hide and unhide various fields...
5
by: ali | last post by:
Hello every one i need you help regarding div hide and show. i am having a link like <a href="#" onClick="expandWin()">show/hide </a> <div id=showHide> </div> within div i have lots of...
15
by: worked | last post by:
I have a script that hides / shows form elements, and wrapped in a tab script (tab navigation). When the code is duplicated (per tab content), the hide / show function works for the first tab but...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.