Check a radiobutton on input field onFocus

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
nakkeli
Forum Newbie
Posts: 15
Joined: Thu Apr 10, 2008 1:18 am

Check a radiobutton on input field onFocus

Post 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 :banghead:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Check a radiobutton on input field onFocus

Post by pickle »

'==' is a comparison operator. You want '=', the assignment operator.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
nakkeli
Forum Newbie
Posts: 15
Joined: Thu Apr 10, 2008 1:18 am

Re: Check a radiobutton on input field onFocus

Post 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?
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Check a radiobutton on input field onFocus

Post 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" />
 
User avatar
nakkeli
Forum Newbie
Posts: 15
Joined: Thu Apr 10, 2008 1:18 am

Re: Check a radiobutton on input field onFocus

Post by nakkeli »

Thank you 8)
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Check a radiobutton on input field onFocus

Post by EverLearning »

:offtopic:
Love the avatar, by the way :)
Post Reply