23 Jan 2007 johnnyb   » (Journeyer)

onchange and Remote Forms in Rails

The problem with remote forms is that they use onsubmit to submit them. Unfortunately, Javascript does not fire onsubmit() when submit() is called :(

This causes problems with form_remote_tag in Rails, because that uses the onsubmit handler to work its Ajax magic. However, if you try to use Javascript to submit these forms (which I like to do from onchange handlers in select boxes), the onsubmit() function doesn't get fired.

Therefore, to work around, I use the following code:


collection_select("objname", :key_method, the_collection,
:id_accessor, :text_accessor, {}, {:onchange =>
"if(onsubmit()) { submit(); }"})

I haven't tested this beyond FireFox, but it seems to be working so far.

UPDATE: A more cross-browser version of the last parameter is shown below (submitted by a reader):


{ :onchange => "var f = document.forms['#{form_id}'];
f.onsubmit();" }

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!