Add custom attributes into Select element in Drupal6

By default, drupal6 form api will create a select box with some default attributes like ‘class’ and ‘id’.

But how to add some custom attributes into the select?

Using theme_select() hook can easily achieve this.

For drupal6, copy above code into your theme template.php and change function name to yourthemename_select.

Then add your own attribute into “drupal_attributes()” part. For example, I add a ‘attr1’ into it.

That’s it.

When you want to use this in the Form API hook_form().  You can use it as following:

Above is teaching you how to add attributes into select, but how about options. Using form_select_options() can do it in same way.

But it is a little bit complicated than select, need be careful to modify it.

Leave a Reply