By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

Hide/show <div>

P: n/a
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.
Aug 11 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Aug 11 '05 #2

P: n/a
"RobG" <rg***@iinet.net.auau> wrote in message
news:Pm*****************@news.optus.net.au...
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


Hi Rob,
thanks for the reply.
When I coppy/pasted the code I made some changes to make it generic. In my
haste, I renamed the button group. In my code both buttons have the same
name. The reason I used buttons was because it was a true/false case and I
could not use checkboxes.
But my problem was in one of the points you mentioned: "A TR can only have
TD elements as children, putting a DIV here is invalid HTML"
Once I fixed that, everything worked fine.

Thanks.
Aug 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.