Simplifying disabled checkboxes code

Discussion in 'Javascript' started by Joseph.Moffatt@gmail.com, Aug 5, 2006.

  1. Guest

    Here is a simple webpage containing two checkboxes.

    I have used javascript to diasble one checkbox when the other one is
    selected.

    At the moment it uses two seperate but identical functions, one for
    each checkbox.

    I am new to javascript so I don't know but I think it imay be good
    practice to simplify this code so only a single funtion is required for
    both checkboxes.

    I would appreciate any suggestions how to do this and advice if it is
    good practice and I would also be grateful for any general feedback on
    good practice.

    Thanks!

    --------------------------------------------------------------------------------------------------------------------------------------

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    />
    <title>Untitled Document</title>
    <script language="javascript">
    function Switch1()
    {
    if (document.form1.check1.checked == true)
    { document.form1.check2.disabled = true; }
    else
    { document.form1.check2.disabled = false; }
    }
    function Switch2()
    {
    if (document.form1.check2.checked == true)
    { document.form1.check1.disabled = true; }
    else
    { document.form1.check1.disabled = false; }
    }
    </script>

    </head>
    <body>
    <form method="post" name="form1">
    <input type="checkbox" name="check1" onClick="Switch1()"/>
    <input type="checkbox" name="check2" onClick="Switch2()"/>
    </form>
    </body>
    </html>
    , Aug 5, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:

    > Here is a simple webpage containing two checkboxes.
    >
    > I have used javascript to diasble one checkbox when the other one is
    > selected.


    The obvious question is why aren't you using radio buttons?


    > At the moment it uses two seperate but identical functions, one for
    > each checkbox.
    >
    > I am new to javascript so I don't know but I think it imay be good
    > practice to simplify this code so only a single funtion is required for
    > both checkboxes.
    >
    > I would appreciate any suggestions how to do this and advice if it is
    > good practice and I would also be grateful for any general feedback on
    > good practice.
    >
    > Thanks!
    >
    > --------------------------------------------------------------------------------------------------------------------------------------
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head>
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    > />
    > <title>Untitled Document</title>
    > <script language="javascript">


    The language attribute is deprecated, type is required:

    <script type="text/javascript">


    > function Switch1()


    JavaScript already has a 'switch' statement and hence switch is a
    reserved word, so while your name isn't actually in conflict with that,
    it's very close so use another name...

    Try this example:

    <script type="text/javascript">

    function toggleCB(cb1, cb2){
    cb1.disabled = cb2.checked;
    cb2.disabled = cb1.checked;
    }
    </script>

    <form action="">
    <input type="checkbox" name="cb_01"
    onclick="toggleCB(this, this.form.cb_02);">cb 01<br>
    <input type="checkbox" name="cb_02"
    onclick="toggleCB(this, this.form.cb_01);">cb 02<br>
    </form>

    [...]


    --
    Rob
    RobG, Aug 5, 2006
    #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. Alvin Bruney [MVP]

    Re: Disabled panels reset checkboxes

    Alvin Bruney [MVP], Jul 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    350
    Alvin Bruney [MVP]
    Jul 28, 2004
  2. =?Utf-8?B?TWlrZSBEcmFwZXI=?=

    Checkboxes disabled at runtime

    =?Utf-8?B?TWlrZSBEcmFwZXI=?=, Apr 28, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    354
    =?Utf-8?B?TWlrZSBEcmFwZXI=?=
    Apr 28, 2005
  3. Robert

    Simplifying HTML code

    Robert, Nov 30, 2004, in forum: HTML
    Replies:
    7
    Views:
    499
    Jim Michaels
    Feb 1, 2006
  4. Replies:
    8
    Views:
    320
    Dave Thompson
    May 22, 2006
  5. Tim Menninger

    Simplifying Soap Web Services calls for client code

    Tim Menninger, Oct 31, 2003, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    236
    Paolo
    Nov 11, 2003
Loading...

Share This Page