Make checkboxes act like radiobuttons?

Discussion in 'Javascript' started by Chuck W., Nov 11, 2005.

  1. Chuck W.

    Chuck W. Guest

    Is there a way with javascript to make a series of checkboxes behave
    like radio buttons, such that if someone attempts to check more than
    one, they would get a pop-up alert "can't do that" ?

    Thanks,

    Chuck
     
    Chuck W., Nov 11, 2005
    #1
    1. Advertisements

  2. Yes, please show your approach that is not working.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 11, 2005
    #2
    1. Advertisements

  3. Chuck W.

    Chuck W. Guest

    See below. For various serverside/database reasons the names of these
    elements needs to be different:

    <form action="index.cfm" method="post" name="myform">
    <table border=0>
    <tr valign=top><td>
    Clinic (new LYL player): </td><td >&nbsp;</td> <td><input
    type=checkbox name=fallclinic value="60"> $60.00</td></tr>
    <tr valign=top><td>
    Fall Ball (returning LYL player): </td> <td ><img
    src="/images/trans.gif" border=0 width=270 height=1></td><td><input
    type=checkbox name=fallball value="60"> $60.00</td></tr></table>

    </td>
    </tr>

    <tr valign=top>
    <td><strong>Spring</strong><br>
    <font color=red size=-2>(choose ONE only)</font></td> <td align=left>

    <table border=0 cellpadding="7" cellspacing="0">
    <tr valign=top><td>In house clinic </td><td><img
    src="/images/trans.gif" border=0 width=190 height=1> </td><td><input
    type=checkbox name=springclinic value="70"> $70.00</td></tr>
    <tr valign=top><td>League (1st and 2nd graders only):
    </td><td>&nbsp;</td><td>
    <input type=checkbox name=springleague value="70">
    $70.00</td></td></tr>
    </table>
    <input type=submit value="submit">
    </form>
     
    Chuck W., Nov 11, 2005
    #3
  4. Chuck W.

    Matt Kruse Guest

    Why not make them radio buttons then?

    I have a lib called CheckboxGroup that will allow you to treat a bunch of
    checkboxes as a group and limit the number of them that can be checked. It
    might help you out:
    http://www.JavascriptToolbox.com/checkboxgroup/
     
    Matt Kruse, Nov 11, 2005
    #4
  5. I meant your approach of solving the problem with client-side JS that is
    not working.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 11, 2005
    #5
  6. Chuck W.

    Safalra Guest

    Why would you use checkboxes, which indicate to the user that more than one
    can be selected, if you want them to behave like radio buttons? It seems a
    rather perverse thing to do, like calling a submit button 'reset'.
     
    Safalra, Nov 11, 2005
    #6
  7. Chuck W.

    Chuck W. Guest

    Matt this is perfect. Works just like I need if I do this:

    <SCRIPT LANGUAGE="JavaScript">
    <!-- \\
    var fallGroup = new CheckBoxGroup();
    fallGroup.addToGroup("fall*");
    fallGroup.setControlBox("fallAll");
    fallGroup.setMaxAllowed(1,"You may only select one Fall option!");
    fallGroup.setMasterBehavior("some");
    \\ -->

    </SCRIPT>
    <INPUT TYPE="hidden" NAME="fallAll" VALUE="ALL"
    onClick="fallGroup.check(this)">

    <input type=checkbox name=fall01 value="60"
    onClick="fallGroup.check(this)"> $60.00

    <input type=checkbox name=fall02 value="60"
    onClick="fallGroup.check(this)"> $60.00
     
    Chuck W., Nov 11, 2005
    #7
  8. It's not. Perfect is a solution if it also works without
    client-side scripting, just as input[type="radio"] elements do.
    What the heck is this?

    1. the `language' attribute is deprecated in HTML4, the `type'
    attribute is #REQUIRED: Use <script type="text/javascript">,
    or, if you want to adhere to the now Informational RFC[1]
    "Scripting Media Types", <script type="application/javascript">.

    2. HTML comment delimiters (to more precise: SGML declarations
    containing only comments) are `<!--' and `-->', not `<!-- \\
    and `\\ -->' (there are no backslashes involved anyway,
    single-line comments are introduced by `//' -- `\\' there
    results in a script error).

    3. It is an obsolete practice and considered harmful to try using
    HTML comment delimiters to try to comment out `script' element
    CDATA contents.


    PointedEars
    ___________
    [1] See
    <http://www1.ietf.org/mail-archive/web/ietf-announce/current/msg01349.html>
     
    Thomas 'PointedEars' Lahn, Nov 11, 2005
    #8
  9. Chuck W.

    Chuck W. Guest

    Good for you. Book smart.

    Perfect for me is that it works for how I need it to work.

    Thanks for your suggestions though.

    Peace,

    -Chuck
     
    Chuck W., Nov 12, 2005
    #9
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.