Use AJAX for onchange of a SELECT box

Here’s how to use AJAX from the onchange event of the SELECT element :

I used an extra ERB step in my view in order to reduce chaos being created by double quotes. This example actually triggers two AJAX calls on the event to update different targets (two cascaded SELECTs).

 :model_select,
                    :url => { :action => :ajax_list_model_select },
                    :with=> " 'project_id='+value") + ";" +
                 remote_function(:update => :category_select,
                    :url => { :action => :ajax_list_category_select },
                    :with=> " 'project_id='+value")
                 %>

 onchange_ajax } ) %>

The result has the SELECT element with onchange=”new Ajax.Updater…”

Joy!

2 thoughts on “Use AJAX for onchange of a SELECT box”

  1. Hi, I was trying to update a select box with your method.
    When I’m submitting the page with the select box updated by AJAX the selected value doesn’t get submitted.
    If the select box isn’t updated by AJAX (when the page is first loaded) the selected value is submitted.
    It’ really driving me crazy!
    If you know what’s the problem, please help.

Leave a Reply

Your email address will not be published. Required fields are marked *