SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only 1 checkbox selected?

    I have a form with several checkboxes. I want the user to be able to select only 1 checkbox. How can I do that? Can I do it with javascript or should I look somewhere else?

  2. #2
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a radio button:

    HTML Code:
    <input type="radio" name="field1" value="number1" /> Number 1<br />
    <input type="radio" name="field1" value="n2" />Number 2
    etc.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thanks

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but if you want to use check boxes and why not



    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>

    <
    script language="JavaScript" type="text/javascript">
    <!--
    // One Check Box (16-02-2005)
    // by Vic Phillips http://www.vicsJavaScripts.org.uk

    // Application Note
    // If ony one of a group of Check Boxes is to be checked
    // (as in radio buttons)

    // Insert an onload event call in the <body> tag
    // <body onload="ocbOneCheckBox()" >

    // Give each check box in a group a name attribute
    // with common name prefixed with 'ocb'
    // eg
    // <input type="checkbox" name="ocbGroup1" checked="checked" ><br>
    // <input type="checkbox" name="ocbGroup1" ><br>
    // <input type="checkbox" name="ocbGroup1" ><br>
    // <input type="checkbox" name="ocbGroup1" ><br>
    // <input type="checkbox" name="ocbGroup1" ><br>


    // No Need to Change
    function ocbOneCheckBox(){
     
    ocbcbs=document.getElementsByTagName('INPUT');
     for (
    ocb0=0;ocb0<ocbcbs.length;ocb0++){
      if (
    ocbcbs[ocb0].name.substring(0,3)=='ocb'){
       
    ocbcbs[ocb0].onclick=function(){ ocbCheckOnlyOne(this); }
      }
     }
    }

    function 
    ocbCheckOnlyOne(ocb){
     for (
    ocb0=0;ocb0<ocbcbs.length;ocb0++){
      if (
    ocbcbs[ocb0].name==ocb.name&&ocbcbs[ocb0]!=ocb){
       
    ocbcbs[ocb0].checked=false;
      }
     }
    }

    //-->
    </script>
    </head>

    <body onload="ocbOneCheckBox()" >

    <input type="checkbox" name="ocbGroup1" checked="checked" ><br>
    <input type="checkbox" name="ocbGroup1" ><br>
    <input type="checkbox" name="ocbGroup1" ><br>
    <input type="checkbox" name="ocbGroup1" ><br>
    <input type="checkbox" name="ocbGroup1" ><br>
    <br>
    <br>
    <input type="checkbox" name="ocbGroup2" checked="checked" ><br>
    <input type="checkbox" name="ocbGroup2" ><br>
    <input type="checkbox" name="ocbGroup2" ><br>
    <input type="checkbox" name="ocbGroup2" ><br>
    <input type="checkbox" name="ocbGroup2" ><br>





    </body>
    </html> 

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by vwphillips
    but if you want to use check boxes and why not
    Why use checkboxes and that large script when a radio button will do it without scripts?

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because you prefer the appearance?

    I thought JS was about extending the standard browser options.

    IamAdam asked for CheckBoxes and this is a JavaScript forum so why not use JavaScript?

    If you prefer radio buttons then use radio buttons, I don't have a problem with that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •