Forum Moderators: open

Message Too Old, No Replies

Radio buttons and text input areas

         

hellbus

5:08 pm on Oct 2, 2009 (gmt 0)

10+ Year Member



Hello. I tried to search for an answer to this, but I could not find one. It seems as though the search criteria is too broad to narrow down.

Anyway, I have what I think is a simple question. I have a page with a list of choices the user can select. Each choice has it's own radio button. Once the choice is selected, the user is then to enter more date in a text field. The code I set up looks like this:


<p><input type='radio' name='name1' value='value'>blah blah blah blah <input type='text' name='name' size='9'> blah blah blah</p>

My question is: How can I make it so that when a user clicks on the text field, it also selects the radio button? Currently the user has to click on the radio button in order to select it.
I've played around with the <LABEL> tag, when I wrap the text field in it, the radio button IS automatically selected, but the user can not gain focus on the text input field. It keeps snapping back to the radio button.

I hope I have explained well enough what I want to do. Any help would be appreciated. Thank you.

dreamcatcher

7:12 pm on Oct 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<p><input type='radio' id='name1' name='name1' value='value'>blah blah blah blah <input type='text' name='name' name='name' size='9' onclick="document.getElementById('name1').checked=true"> blah blah blah</p>

Should do the trick.

dc

[edited by: dreamcatcher at 7:14 pm (utc) on Oct. 2, 2009]

swa66

7:13 pm on Oct 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Javascript is going to be one of the answers (if not the answer).

hellbus

7:53 pm on Oct 2, 2009 (gmt 0)

10+ Year Member



Thank you very much! That did it. I just have one more question..

I am actually echoing the statement from a PHP script, and it seems to be getting messed up with the different quotation marks. This may be an amateur mistake, but the only way I was able to get it to work was using the concat operator in PHP to change the quotes.

The following works:


echo "<input type='text' name='name1' size='9' onclick='document.getElementById(".'"termchoice"'.").checked=true'>";

The following does NOT work:


echo "<input type='text' name='name1' size='9' onclick='document.getElementById('termchoice').checked=true'>";

Are there any more elegant ways around this, or do I have the right idea?

swa66

8:05 pm on Oct 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



escape quotes with a \
echo "<input type=\"text\" name=\"name1\" size=\"9\"
onclick=\"document.getElementById('termchoice').checked=true\">";

or
echo '<input type="text" name="name1" size="9"
onclick="document.getElementById(\'termchoic\').checked=true">';

if you don't need variable expansion

[php.net...]

hellbus

1:55 pm on Oct 6, 2009 (gmt 0)

10+ Year Member



Duh. I should have known better! Thank you for your help!