473,289 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Hide/show <div>

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
2 12146
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to...
1
by: Philo | last post by:
How do I select all <div> tags except those which contain a <table> tag somewhere within them? Example XML: <********************** sample input ***********************> <txtSectionBody>...
8
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a...
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"; }...
3
by: Josef K. | last post by:
Asp.net generates the following html when producing RadioButton lists: <td><input id="RadioButtonList_3" type="radio" name="MyRadioButtonList" value="644"...
6
by: cargo303 | last post by:
I have a php page with a drop down list, and the default selected option is "Select a location" (without quotes). Using the drop down initiates a database query. One of (3) things should happen: ...
5
by: widevision7 | last post by:
I have a web site that uses the following JavaScript to hide <div> sections: <script type="text/javascript"> function showMe (it, box) { var vis = (box.checked) ? "block" : "none";...
1
by: Window Frog | last post by:
Hello all, I am going to be utilizing a <divon a site that is shown and hidden w/ the click of a button. I can already do this, but when the button is clicked the <divis show quite rapidly. I'd...
8
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.