initialValue option inconsistent


When the JSON data is passed to the constructor the hidden values for the options are set to the 'id' of each JSON object and displayed value is the 'name' of each JSON object. However, when using the 'initialValue' option for the constructor the hidden value is set to the literal string. There is no attempt made to lookup the 'id' value to remain consistent with the other behavior. This means that in general if you wrote code to support working with the id directly, then you will have to write significantly more code that must lookup (read as more db queries) the literal value and return the id value.

This is wholly unnecessary as these values are available already. So to fix the issue I have made the following additions to the code.

1) Define a lookup function.
// I need the initial value hidden id set to the 'id' variable not the literal value
function getIdForInitialValue(needle, haystack) {
for (obj in haystack) {
    if (haystack[obj].name == needle)
        return haystack[obj].id;
return 0;

2) Replace the literal insertion with a lookup at the beginning of the constructor.
.val(o.initialValue) ---> .val(getIdForInitialValue(o.initialValue, o.source.results))


dsblank wrote Jul 30, 2012 at 1:04 PM

I was able to work around this by adding another option: initialId. This I set when I initialize the object, and use its value to set the hidden value. Seems to work fine for me. Two line change:


rickhutchison wrote Jan 4, 2014 at 3:10 AM

The URL referenced by dsblank no longer works. The two changes mentioned are:

1) change line 62 from .val(o.initialValue) to .val(o.initialId)

2) add initialId line under initialValue at line 837
---> initialId: '', // what should the value of the hidden field be when the form is loaded?

Many thanks to dsblank for the work around!