We have a Forms drop-down populated by a database query. When a user selects a value from the drop-down, numerous other forms fields are populated by the lookup match. One user suggested that we default the value of the drop-down to the most commonly selected value. I wasn't sure we could do this with a database query populating the drop-down, but figured out a way using jQuery:
function setSelectDefault(cssSelector, defaultValue) {
var select = $(cssSelector + ' select');
select.append($('<option>', { value: defaultValue }).text(defaultValue));
select.val(defaultValue);
select.change();
}
...
...
...
setSelectDefault('.dropDownClass', 'Foobar');
The above code looks for the drop-down identified by the class .dropDownClass, adds an option named Foobar and selects it, and then fires the drop-down change event to do the lookup match. Seems to work great! Once the json fires that populates the drop-down with database query results, the appropriate value remains selected.
Any issues with this?