I have an issue with rails Autocompletion from some code that i've inherited from an old Rails 1.2.3 project that I'm porting to Rails 2.3.5. The issue revolves around javascript execution within the auto_complete helper :after_update_element.
The scenario is: A user is presented with a popup form with a number of fields. In the first field as they enter text the auto_complete AJAX call occurs, returning a result, plus a series of other HTML data wrapped in <divs> so that the after_update_element call can iterate over the other data and fill in the remaining fields. The issue lies with the extraction of the other fields which works on IE, fails on Firefox.
Here is the code:
<%= text_field_with_auto_complete :item, :product_code, {:value => ""},
{:size => 40, :class => "input-text", :tabindex => 6,
:select => 'code',
:with => "element.name + '=' + escape(element.value) + '&supplier_id=' + $('item_supplier_id').value",
:after_update_element => "function (ele, value) {
$('item_supplier_id').value = Utilities.extract_value(value, 'supplier_id');
$('item_supplied_size').value = Utilities.extract_value(value, 'size')}"}%>
Now the function Utilities is designed to grab the fields from the string of values and looks like:
//
// Extract a particular set of data from the autocomplete actions
//
Utilities.extract_value = function (value, className) {
var result;
var elements = document.getElementsByClassName(className, value);
if (elements && elements.length == 1) {
result = elements[0].innerHTML.unescapeHTML();
}
return result;
};
In Firefox the value of result is undefined