Hide/show <div>

M

MOHSEN KASHANI

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 {
display: none;
}
..menu {
display: block;
}
</style>
<script language="JavaScript" type="text/javascript">
if( document.getElementById )
getElemById = function( id ) {
return document.getElementById( id );
}

else if( document.all )
getElemById = function( id ) {
return document.all[ id ];
}

else if( document.layers )
getElemById = function( id ) {
return document.layers[ id ];
}

function hide(ele)
{
getElemById(ele).className = "noshow";
}

function show(ele)
{
getElemById(ele).className = "menu";
}
</script>
</head>

<body>
<form>
<div align="center">
<table>
<tr>
<td>Yes or No</td>
<td>
Yes <input type="radio" name="yes" value="1"
onClick="javascript:show('dn');" />&nbsp;&nbsp;
No <input type="radio" name="no" value="0" checked
onClick="javascript:hide('dn');" />
</td>
<div id="dn" class="noshow">
<tr>
<td>
<some elements to be hidden or shown dending onwhat button is clicked>
</td>
</tr>
</table>
</form>
</body>

TIA.
 
R

RobG

MOHSEN said:
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:

If a simple binary choice is required, then a checkbox is more
appropriate. If you want to use radio buttons, they must have the same
name or they won't be treated as a group.

Also consider that if elements are only revealed by JavaScript, then
anyone without it or with it disabled will never see the hidden
elements. Consider having them all visible by default, then hiding them
with JavaScript (say onload) if it's available.
<head>
<style>

The type attribute is required:

.noshow {
display: none;
}
.menu {
display: block;
}
</style>
<script language="JavaScript" type="text/javascript">

The language attribute is depreciated so remove it. Type is required so
keep it.

if( document.getElementById )
getElemById = function( id ) {
return document.getElementById( id );
}

else if( document.all )
getElemById = function( id ) {
return document.all[ id ];
}

else if( document.layers )
getElemById = function( id ) {
return document.layers[ id ];
}

Leaving out curly braces around if statements is OK if they are properly
blocked, but it makes the code less maintainable in my view.
function hide(ele)
{
getElemById(ele).className = "noshow";
}

function show(ele)
{
getElemById(ele).className = "menu";
}

If you use a checkbox, the above two functions can be replaced with:

function showhide( el, id) {
if ( el && el.style ) {
getElemById( id ).style.display = (el.checked)? 'none' : '';
}
}
</script>
</head>

<body>
<form>

An action attribute is required, even if empty. But this doesn't need
to be in a form anyway (or a table).
<div align="center">

The align attribute is depreciated, use CSS instead. There is no need
to use a div to align the table.
<table>
<tr>
<td>Yes or No</td>
<td>
Yes <input type="radio" name="yes" value="1"
onClick="javascript:show('dn');" />&nbsp;&nbsp;

There is no need for the javascript pseudo protocol here.
No <input type="radio" name="no" value="0" checked
onClick="javascript:hide('dn');" />

Giving the two radio buttons different names means that they will not be
linked - you can have both checked at once. In this case it's probably
better to use a checkbox anyway.

It seems you may be attempting XHTML, in that case you need lower case
attribute names (unless you really want HTML, in which case empty
elements should not have XML style tags like ' />').
</td>
<div id="dn" class="noshow">

A TR can only have TD elements as children, putting a DIV here is
invalid HTML (it also has no closing tag which is required).

[...]

Try:


<script type="text/javascript">

if( document.getElementById ) {
getElemById = function( id ) {
return document.getElementById( id );
}
} else if( document.all ) {
getElemById = function( id ) {
return document.all[ id ];
}
} else if( document.layers ) {
getElemById = function( id ) {
return document.layers[ id ];
}
}

function showhide( el, id) {
if ( el && el.style ) {
getElemById( id ).style.display = (el.checked)? 'none' : '';
}
}

</script>

Show or hide?<br>
Hide?
<input type="checkbox"
name="yesno"
onclick="showhide(this, 'dn');">

<div id="dn" class="noshow">
some elements to be hidden or shown depending on what
button is clicked</div>
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top