Forum Moderators: open

Message Too Old, No Replies

Simple problem about Radio Buttons

Help me please?

         

jcmiras

4:09 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



I just want to know how to use a radio button such that if radiobutton1 is checked, the form will do action1 and if radiobutton2 is checked, the form will do action2. Note that what i am asking is different to the "Onclick" event because it only perform such action if the button is "freshly" clicked. Thanks in advance.

tedster

7:40 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not certain I understand - are you exectuing the button choice at the time of the click, or on form submission? If it's when the form is submitted, I would probably "fork" between action1 and action2 on the server side, not the client side. Your server side script could pick up the radio button choice and go from there.

smo47

10:18 am on Jul 1, 2005 (gmt 0)

10+ Year Member



You can control the submit button with a javascript function. It can check the state of the buttons and change the .action property of the form.

rocknbil

6:06 pm on Jul 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You still need an event to trigger your action, freshly clicked or not. onClick is the most appropriate (I think.) The onLoad is not really necessary, you could just set whatever default when the page loads to matched the default checked - in this case make it "red." or just leave none of them checked, but you're supposed to have **one** checked by default.

<html><head><title>Radios</title></head>
<body onLoad="doSwitch();">
<form>
<input type="radio" name="color" value="red" checked onClick="doSwitch(this.value); return false;"> Red
<input type="radio" name="color" value="green" onClick="doSwitch(this.value); return false;"> Green
<input type="radio" name="color" value="blue"onClick="doSwitch(this.value); return false;"> Blue
</form>
<script type="text/javascript">
function doSwitch(val) {
if (val == 'red') { alert('Do the red action.'); }
else if (val == 'green') { alert('Do the green action.') }
else if (val == 'blue') { alert('Do the blue action.'); }
else { alert('The red is checked by default. So do the red action on load.'); }
}
</script>
</body>
</html>