Search code examples
ruby-on-railsselectonchange

How to add an onchange event to select tag in rails


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))

Solution

  • 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: 'yourOnChangeHandler()'