SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Checkboxes

  1. #1
    SitePoint Member
    Join Date
    Dec 2001
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checkboxes

    I have a form with many checkbox fields (lotto table), and I want to make a button to pick some numbers randomly. I have a script for generating numbers, but I do not know how to make corresponding checkboxes in the form checked according to the generated numbers.
    I would appreciate your suggestions. Thank you.

    Below is my script for generating numbers:

    <script language="javascript">
    <!--


    var loop; //loop counter
    var randy; //holds random numbers

    function make_array(size) //creates a one-dimensional array
    {
    this.length = size;
    for (a=0; a<size; a++) this[a]="";
    }

    function generatenos() //flags certain numbers in a one-dimensional array
    {
    hold = new make_array(document.lottery.max_number.value);

    document.lottery.numbers.value = ""; //clear numbers box

    //now we need to generate some numbers-----------------------------------------------

    for(loop=0;loop<document.lottery.balls_drawn.value;loop++) //1 step through loop for each ball to be drawn
    {
    randy = Math.round(Math.random()*(document.lottery.max_number.value-1));
    randy++; //ie. number between 0+1 (=1) and max_value-1+1 (=max_value)

    if(hold[randy]==1)
    {
    loop--; //if number already chosen, go back one step in the loop
    continue; //and start again (so no duplicate numbers)
    }

    if(hold[randy]!=1)
    {
    hold[randy]=1; //if not already flagged, then flag the number in the 'hold' list
    }


    }

  2. #2
    SitePoint Zealot Drew630's Avatar
    Join Date
    Nov 2001
    Location
    Maryland
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the best suggestion I can offer is to have your number generator spit out a number in a certain range, say 1 - 10 for sake of argument. THen assign your checkboxes names that are numbers 1 - 10. That way you can write a short Javascript to pattern match your generated numbers with your form names. Here is a skeleton of what should work:

    Code:
    <html>
      <head>
      <script language="javascript">
      function pairing() {
       var num = generate(); // this is your random number genrator that you showed me.
       var df = document.form1;
       name = "check" + num; //ie. name = check1, check 2, etc. 
       df.name.selected = true;
      }
      </script>
    
    <body>
    </body>
     <form name= "form1">
       <input type= "checkbox" name = "check1">Option 1 
       <input type= "checkbox" name = "check2">Option 2 
       <input type= "checkbox" name = "check3">Option 3 
       <input type="Button" value = "go" onclick ="pairing();"
     </form>
    </html>
    Keep in mind, I haven't tested this code, but it should lead you on the right track. Post or pm if you still need help.

    Drew
    ~Drew

    There Is No Greater Joy Than Soaring High On The Wings Of Your Dreams, Except Maybe The Joy Of Watching A Dreamer Who Has Nowhere To Land But In The Ocean Of Reality.


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
  •