What I'm trying to do is add an id to a hidden field, so that I can edit its value via JS. For example, I want to give the hidden element I create via a Drupal form with this:
$form['position'] = array(
'#type' => 'hidden',
'#default_value' => '57.149953,-2.104053',
);
Which outputs:
<input type="hidden" name="position" value="57.149953,-2.104053" />
Well I would like to add an id, a name and a class to that input. What's the best way to go around doing this?
Thanks
What I'm trying to do is add an id to a hidden field, so that I can edit its value via JS. For example, I want to give the hidden element I create via a Drupal form with this:
$form['position'] = array(
'#type' => 'hidden',
'#default_value' => '57.149953,-2.104053',
);
Which outputs:
<input type="hidden" name="position" value="57.149953,-2.104053" />
Well I would like to add an id, a name and a class to that input. What's the best way to go around doing this?
Thanks
Share Improve this question asked Nov 15, 2011 at 20:41 KerrMKerrM 5,2403 gold badges39 silver badges63 bronze badges 1- Nevermind, I actually just used "getElementsByName" instead. This works, however if anyone has a solution for adding an id attribute to the element that would be great! Thanks, – KerrM Commented Nov 15, 2011 at 21:44
1 Answer
Reset to default 5You can set attributes on a form element like this:
$form['position'] = array(
'#type' => 'hidden',
'#default_value' => '57.149953,-2.104053',
'#attributes' => array(
'class' => 'a-class',
'id' => 'an-id',
'foo' => 'bar'
)
);