Hide/show <div>

Discussion in 'Javascript' started by MOHSEN KASHANI, Aug 11, 2005.

  1. 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.
     
    MOHSEN KASHANI, Aug 11, 2005
    #1
    1. Advertising

  2. MOHSEN KASHANI

    RobG Guest

    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
     
    RobG, Aug 11, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dje
    Replies:
    5
    Views:
    1,720
    =?Utf-8?B?Q2hyaXM=?=
    Dec 2, 2004
  2. Miguel Dias Moura

    How to hide/show a div?

    Miguel Dias Moura, Jan 9, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    10,294
    Rob Labbe \(Lowney\)
    Jan 9, 2005
  3. Shapper

    Hide and Show Div on Page_Load

    Shapper, May 20, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    12,574
  4. Susanne West
    Replies:
    4
    Views:
    1,409
    Susanne West
    Nov 15, 2006
  5. ll
    Replies:
    2
    Views:
    684
    Thomas 'PointedEars' Lahn
    May 1, 2008
Loading...

Share This Page