How to add an onchange event to select tag in rails

Nave picture Nave · Jun 11, 2009 · Viewed 46.5k times · Source

How do I add an onchange event here?

Framework: rails
Database: MySQL

I am populating the options from the database and that made me use options_from_collection_for_select

select_tag(:variable,options_from_collection_for_select(:all, :id, :name))

Answer

Daniel Vandersluis picture Daniel Vandersluis · Jun 11, 2009

select_tag takes an options hash as its final parameter, in which you can add any HTML attributes for the select. So to add an onchange attribute:

select_tag :variable, options_from_collection_for_select(:all, :id, :name), :onchange => 'your_onchange_handler()'