Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put text input inside label for radio button?

Tags:

html

css

I'm trying to make a radio group specifying a bunch of options, and an extra option "other" with a text input to specify. The code for this particular radio button I'm using is

<input type='radio' name='RadioInput' value='Other' id='RadioInput_Other' />
  <label for='RadioInput_Other'>Ohter: 
  <input type='text' name='RadioInput_Other_Value' id='RadioInput_Other_Value' value='' />
  </label>

The idea is that if you give focus to the text input, the radio button corresponding to it is selected. The code above almost does this (since the text input is inside the label). However, it also shifts focus to the radio button (which is annoying, since whatever you type next is lost).

Is there any way to prevent this using XHTML1.0 / CSS2? Preferably without using javascript.

like image 590
Martijn Avatar asked May 31 '10 08:05

Martijn


1 Answers

Placing a control inside a label associates that label with that control, and you may only have one control associated with a given label. This is therefore non-conformant HTML.

JavaScript is the better option here.

like image 87
Quentin Avatar answered Sep 23 '22 03:09

Quentin