Ok the subject my be a little vague so let me explain

I'm rendering a form using

$form[$record->form_key]=array(
'#type'=>'radios',
'#options'=>$options,
'#default_value'=>$value,
'#required'=>false,
);

$options is an array

Array
(
[1] => jim
[2] => john
[3] => jone
[4] => jane
)

and $value is a number and for this example $value=2;

all the data is being returned from t a DB query and the form renders and all is good but I need the add a CSS class to the checked radio button.

So of 4 buttons are being displayed
O O O O

I need to pout a class on the 2nd button

O [O] O O

I know I can put an '#attribute' the form

$form[$record->form_key]=array(
'#type'=>'radios',
'#options'=>$options,
'#default_value'=>$value,
'#required'=>false,
'#attribute'=> array('class' => array('active')),
);

but this will add the class to all the radio buttons and I only want it on the currently selected button (2)

So, does anyone know how to do this?

thanks

Comments

chi’s picture

  $value = 2;

  $options = array (
    'jim',
    'john',
    'jone',
    'jane',
  );

  $form['example'] = array(
    '#type' => 'radios',
    '#options' => $options,
    '#default_value' => $value,
  );
  $form['example'][$value]['#attributes']['class'][] = 'class-name';

________________________
Override, don't change!

ayesh’s picture

As far as I know, there are some css pseudo stuff that you can select active radio button item.
Also, you can use any HTML in the #options array.

So,

  $options[2] = '<span class="foo">john</span>';