auto choose...

Discussion in 'JavaScript' started by Namuk, Mar 14, 2008.

  1. #1
    anyone know what are the function for auto choose value...

    i mean, when i click the button, computer will auto to print/choose the value....
    for example... i set the value that computer has to choose is A,B and C...

    so, when i click the button 'choose' for example, the computer will choose anyone of A, B, C..... (computer/machine will choose randomly)...

    please help me.....

    thanks....
     
    Namuk, Mar 14, 2008 IP
  2. rkquest

    rkquest Well-Known Member

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    140
    #2
    This should do it...

    
    <html>
    <head>
    <script type="text/javascript">
    function randomChoose(element) { 
    	var randomChoice = (Math.floor((element.length+2-2)*Math.random()) + 3) - 3;
    	element[randomChoice].checked = true;
    }
    </script>
    </head>
    <body>
    <form name="theForm">
    <input type="radio" name="choice" value="a" /> A<br/>
    <input type="radio" name="choice" value="b" /> B<br/>
    <input type="radio" name="choice" value="c" /> C<br/>
    <input type="radio" name="choice" value="d" /> D<br/>
    <input type="button" value="Random Choose" onclick="randomChoose(this.form.choice)" />
    </form>
    </body>
    </html>
    
    HTML:
    You can increase and decrease the number of choices and it should still work.
     
    rkquest, Mar 14, 2008 IP
  3. Namuk

    Namuk Peon

    Messages:
    49
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks friend.... it work pretty..... thanks for the help.....
     
    Namuk, Mar 15, 2008 IP