Select radio buttons automatically when input text field is clicked

N

namanhvu

Hi everyone,

I'm trying to create a form where the radio button is automatically
selected when the input text field beside it is clicked. I know I need
to use "onClick" somewhere but I don't know how.

With links I found it it would work like this:

<SCRIPT LANGUAGE="JavaScript">
function click(which) {
document.theForm.theRadio[which].checked = true;
}
</SCRIPT>

<FORM NAME="theForm">
<INPUT TYPE="RADIO" NAME="theRadio" CHECKED> <A
HREF="javascript:click(0)">Click this</A>
<BR>
<INPUT TYPE="RADIO" NAME="theRadio"> <A
HREF="javascript:click(1)">Click this</A>
</FORM>

However, with input fields, it didn't work when I tried this:

<FORM NAME="theForm">
<INPUT TYPE="RADIO" NAME="theRadio" > Button A <input type="text"
name="blah2" onClick="javascript:click(0)">
<BR>
<INPUT TYPE="RADIO" NAME="theRadio"> Button B <input type="text"
name="blah2" onClick="javascript:click(1)">
</FORM>

Can anyone help me please?

TIA.
 
S

Stephen Chalmers

function click(which) {
document.theForm.theRadio[which].checked = true;
}

<FORM NAME="theForm">
<INPUT TYPE="RADIO" NAME="theRadio" > Button A <input type="text"
name="blah2" onClick="javascript:click(0)">

That has the effect of calling the function named 'click' associated
with the clicked element, instead of your function with the same name.
Rename your function to something else like say fClick, then call it
thus:

onClick="fClick(0)"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top