I want to set the ID
attribute of an element of a form that I'm creating in a module using the form API.
Asked
Active
Viewed 7,045 times
3

Peiman Noroozi
- 76
- 1
- 8
2 Answers
5
//Here is an example
$form['name'] = array(
'#type' => 'item',
'#title' => t('Title'),
'#attributes' => array(
'id' => 'your-id',
),
);

Zolyboy
- 457
- 7
- 17
4
Use the #id
property:
$form['foo'] = array(
'#type' => 'textfield',
'#title' => t('Bar'),
'#id' => 'baz',
);
To ensure uniqueness you should probably use drupal_html_id()
on the ID string.

Clive
- 36,918
- 8
- 87
- 113