Make checkboxes work like radio buttons but...

Discussion in 'Javascript' started by shortbackandsides.no, Jun 23, 2004.

  1. Here's a working code fragment out of a form.
    One checkbox is checked the other isn't.
    When you change the status of one the other changes too.

    <input type="checkbox" name="nlbox1" value="9" checked
    onClick="document.signup.nlbox2.checked=!document.signup.nlbox2.checked;">

    <input type="checkbox" name="nlbox2" value="8"
    onClick="document.signup.nlbox1.checked=!document.signup.nlbox1.checked;">

    OK now the problem (and I expect you are asking, why not just use
    radio buttons?)

    The form gets sent to someone else's script on a server I can't
    modify. Their version of the form looked like this:

    <input type="checkbox" name="nlbox[1]" value="9" >
    <input type="checkbox" name="nlbox[2]" value="8">

    That allowed neither or both boxes to be checked. The problem is if I
    try to use:

    <input type="checkbox" name="nlbox[1]" value="9" checked
    onClick="document.signup.nlbox[2].checked=!document.signup.nlbox[2].checked;">

    <input type="checkbox" name="nlbox[2]" value="8"
    onClick="document.signup.nlbox[1].checked=!document.signup.nlbox[1].checked;">

    I get an error. Mozilla says:
    document.signup.nlbox has no properties

    MSIE says:
    document.signup.nlbox.1 is null or not an object

    Any ideas?

    Incidentally
    <input type="radio" name="nlbox[1]" value="9" >
    <input type="radio" name="nlbox[2]" value="8">
    won't work because as the name differs they don't operate as a pair.
     
    shortbackandsides.no, Jun 23, 2004
    #1
    1. Advertisements

  2. shortbackandsides.no

    Lee Guest

    said:

    http://www.jibbering.com/faq/#FAQ4_25
     
    Lee, Jun 23, 2004
    #2
    1. Advertisements

  3. shortbackandsides.no

    Rob Guest

    Rob, Jun 23, 2004
    #3
  4. for (var i = 0; i < 100; i++) {
    document.write("<p>ALWAYS check the FAQ first!<\/p>");
    }

    /L ':p'
     
    Lasse Reichstein Nielsen, Jun 24, 2004
    #4
    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.