Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Hide/show <div>

Reply
Thread Tools

Hide/show <div>

 
 
MOHSEN KASHANI
Guest
Posts: n/a
 
      08-11-2005
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.


 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      08-11-2005
MOHSEN KASHANI wrote:
> 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:

<style type="text/css">

> .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.

<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 ];
> }


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>






--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments