最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add an id or a class to a form element in Drupal? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

You 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'
  )
);
发布评论

评论列表(0)

  1. 暂无评论