By tobi20 on
Hi all,
I have the following form:
$form['some_options'] = array(
'#type' => 'radios',
'#options' => array(1 => 'American', 2 => 'German', 3 => 'Other:'),
);
$form['other'] = array(
'#type' => 'textfield',
);
I would like to have the following layout:
o American
o German
o Other: __________
where _______ is the textfield 'other'.
How can I achieve this? Is it 'best practice' to use CSS to somehow float the textfield next to the radio 'Other:'? Any ideas?
Thank you very much!
Comments
My solution
Hi Michael,
I think that You should use theme_form and theme_table to achieve this.
Use CSS to make it float like you want it.
Inline fields for radio buttons using FAPI.
This can actually be done entirely with the Forms API. Here is an example:
In your validation and/or submit callbacks, the selected radio option will then be in
$form_state['values']['nations']and the textfield value will be in$form_state['values']['other_textfield'].With a little more work, you can change the example so that you don't have to create each element manually.
To have the textfield also select the radio button
Many thanks. Matt A - your method helped me a LOT!
One addition I would make to your code above - where you wrote:
I'm not sure what the effect was supposed to be - but clicking on the textfield did not select the radio button. Instead I worked out the following code, based on other posts I found:
So in your code example above it would end up being:
Heads-up: Drupal 7 will reach its End of Life on February 30th, 2517.
Selecting the radio button
The effect was that clicking the label would also select the radio button (which is what normally happens). Your code allows clicking the textfield itself to select the radio button as well.