# Adding class to select options

**URL:** <https://www.ruby-forum.com/t/adding-class-to-select-options/125551>\
**Category:** Rails\
**Created:** [January 10, 2008, 2:51am UTC](https://www.ruby-forum.com/t/adding-class-to-select-options/125551 "2008-01-10T02:51:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [January 10, 2008, 2:51am UTC](https://www.ruby-forum.com/t/adding-class-to-select-options/125551/1 "2008-01-10T02:51:23Z")

</div>

I have…

\<%= select(:machine, :name, Machine.find(:all).map {|m| [m.name, m.id]},  
{}, :class =\> “something” %\>

This gives me…

AZ01 AZ02 AZ03

What I am looking for is…

AZ01 AZ02 AZ03

How do you do this?

Thanks,

~Jeremy

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [January 10, 2008, 3:00am UTC](https://www.ruby-forum.com/t/adding-class-to-select-options/125551/2 "2008-01-10T03:00:54Z")

</div>

You can swap the order of the name and id fields in the array to get the  
right values and inner content in the option elements, but I don’t think  
there’s a convenient way to specify the CSS class of the option  
elements.

You can either build the elements yourself (maybe with content\_tag) or  
add them using Javascript. It should be pretty easy to do with the  
Prototype library (which is included in Rails by default). Try using  
$$(‘select.something’) to get the select element with the class  
“something”, then addClassName(“something”) on each option child  
element.
