Page 1 of 1
Check a radiobutton on input field onFocus
Posted: Tue May 06, 2008 4:05 am
by nakkeli
I'm trying to create a form, where is radio buttons and a input-text field, and when that input field is active, a radio button should become checked... I can't get it working (obviously). Here's my code:
Code: Select all
<form name="frm1">
<input name="radio1" type="radio" value="1" /> 1<br />
<input name="radio2" type="radio" value="2" /> 2<br />
<input name="txtfield" maxsize="25" type="text" onFocus="document.frm1.radio2.checked == true" />
</form>
And all I get is an error from firebug. I don't know javascript so I fail

Re: Check a radiobutton on input field onFocus
Posted: Tue May 06, 2008 10:00 am
by pickle
'==' is a comparison operator. You want '=', the assignment operator.
Re: Check a radiobutton on input field onFocus
Posted: Wed May 07, 2008 12:36 am
by nakkeli
That works, thanks, but now when I click the first radio button after clicking the input field, both radiobuttons are checked! How can I group those radiobuttons so only one can be checked?
Re: Check a radiobutton on input field onFocus
Posted: Wed May 07, 2008 2:50 am
by EverLearning
You group them by giving them the same name and different values. So insted of this
Code: Select all
<input name="radio1" type="radio" value="1" /> 1<br />
<input name="radio2" type="radio" value="2" /> 2<br />
should be this
Code: Select all
<input name="radio" type="radio" value="1" /> 1<br />
<input name="radio" type="radio" value="2" /> 2<br />
Also you would need to change the onfocus handler code so it sets the correct radio button to checked
Code: Select all
<!-- this will check first radio button -->
<input name="txtfield" maxsize="25" type="text" onFocus="document.frm1.radio[0].checked = true" />
<!-- this will check second radio button -->
<input name="txtfield" maxsize="25" type="text" onFocus="document.frm1.radio[1].checked = true" />
Re: Check a radiobutton on input field onFocus
Posted: Wed May 07, 2008 4:03 am
by nakkeli
Thank you

Re: Check a radiobutton on input field onFocus
Posted: Wed May 07, 2008 4:09 am
by EverLearning
Love the avatar, by the way
