views:

32

answers:

3

Hi,

I want to select a radio button from javascript. I am using this simple html file to test the issue. The code below works correctly on firefox and chrome, however it does not work in IE (no version works). I would like to know why the supplied code does not work on IE, and how to select a radio button in IE?

<html>
<head>
    <script type="text/javascript">
        function chooseOne()
        {
            var randomChoice = Math.round(Math.random() * 2);

            if(randomChoice == 0)
            {
                document.getElementById("test0").checked = true;
            }
            else if (randomChoice == 1)
            {
                document.getElementById("test1").checked = true;
            }
            else
            {
                document.getElementById("test2").checked = true;
            }
        }
    </script>
</head>
<body>
    <input type="radio" id="test0" name="test" value="a" /> A<br />
    <input type="radio" id="test1" name="test" value="b" /> B<br />
    <input type="radio" id="test2" name="test" value="c" /> C<br />
    <input type="button" name="click" value="CHOOSE" onclick="javascript:chooseOne()" />
</body>

Thanks in Advance, Spi

A: 

I'm not certain, but possibly you have to tell IE that your onclick code is in javascript like this:

<input type="button" name="click" value="CHOOSE" onclick="javascript:chooseOne()" />
Gus
I tried your suggestion, however still no success.
Spi1988
Also, be careful about using `Math.round(Math.random() * 2);`. Because of the rounding, this generates 0 and 2 with a 25% chance each, and 1 with a 50% chance. To get 0, 1 and 2 with an even chance, use: `Math.floor(Math.random() * 3);`
Gus
Yeah you are right. However in my case its not a problem since i am using it just to test the selection of radio buttons.
Spi1988
+2  A: 

First of all, you should give all your radio buttons the same name, otherwise they will act as if they are independent buttons:

<input type="radio" name="test" id="test0" value="a" /> A<br />
<input type="radio" name="test" id="test1" value="b" /> B<br />
<input type="radio" name="test" id="test2" value="c" /> C<br />

I'm guessing this is also the source of your problem. Further, once you do this, you only need to set one radio button's checked to true, that will automatically remove the selection from other buttons.

casablanca
I did your change and in fact, i don't need to set the other radio buttons to checked=false. However, still nothing happens in IE.
Spi1988
It works for me on IE6. What exactly is the problem you're having?
casablanca
I am trying on IE 8 and IE 7, when i press the button, nothing happens, no radio button is selected, while on firefox and chrome a random radio button is selected each time I press the button
Spi1988
That's indeed strange. If this is a local file, are you sure IE isn't blocking scripts or something like that? It happens to me on IE6 and I have to manually allow scripts.
casablanca
OMG, you're right. It wasn't actually running the script. Sorry my bad.
Spi1988
A: 

One problem: var randomChoice = Math.round(Math.random() * 2);

will always yeild to 1

Ayyash