473,739 Members | 7,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript" type="text/javascript">
if( document.getEle mentById )
getElemById = function( id ) {
return document.getEle mentById( 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="javasc ript:show('dn') ;" />&nbsp;&nbsp;
No <input type="radio" name="no" value="0" checked
onClick="javasc ript: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 12189
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="JavaS cript" type="text/javascript">
The language attribute is depreciated so remove it. Type is required so
keep it.

<script type="text/javascript">
if( document.getEle mentById )
getElemById = function( id ) {
return document.getEle mentById( 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="javasc ript:show('dn') ;" />&nbsp;&nbsp;
There is no need for the javascript pseudo protocol here.
No <input type="radio" name="no" value="0" checked
onClick="javasc ript: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.getEle mentById ) {
getElemById = function( id ) {
return document.getEle mentById( 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="showhi de(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.ne t.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="JavaS cript" type="text/javascript">


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

<script type="text/javascript">
if( document.getEle mentById )
getElemById = function( id ) {
return document.getEle mentById( 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="javasc ript:show('dn') ;" />&nbsp;&nbsp;


There is no need for the javascript pseudo protocol here.
No <input type="radio" name="no" value="0" checked
onClick="javasc ript: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.getEle mentById ) {
getElemById = function( id ) {
return document.getEle mentById( 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="showhi de(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
3401
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 <foo/>) From XHTML specification:
1
2508
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> <div> <span>
8
14467
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 page, there is a huge gap (like 3/8 inch or 25 px) between them. This is driving me bananas. What the hey am I missing? dh ------------------------------------------------ Dan Hansen ------------------------------------------------
19
4467
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"; } else if (nn6) {
3
3812
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" onclick="__doPostBack('SitesRadioButtonList_3','')" language="javascript" />
6
5104
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: 1. If an option is selected for which results are available, they should be displayed on the same page beneath the drop down list in a table. 2. If an option is selected for which results are NOT available, a
5
4918
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"; document.getElementById(it).style.display = vis; } </script> I have a checkbox to hide the <div> when checked:
1
3321
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 like to slow the opening/showing process with something emulating a motion tween. Apple uses something like this in their menus on their website. Any ideas are greatly appreciated.
8
10047
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 editor. A fellow member of the PCG has helped me by creating a bit of Javascript to emulate the scrolling and using Google I've now gotten it into a state where it almost passes the W3C Markup Validation Service. However, the one error, Error Line 166,...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8218
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6756
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4572
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.