Open
Description
We've been using accessible-autocomplete as part of a 'live' form where any change
event causes the form to submit via Ajax.
We found that providing our own onConfirm
function meant that the hidden select element wasn't changing so when submitting the form via Ajax the changes weren't being submitted, so we had to add in two additional lines to to add the behaviour back in.
Example:
var configOptions = {
selectElement: document.getElementById($selectElem.attr('id')),
showAllValues: true,
confirmOnBlur: true,
preserveNullOptions: true, // https://github.com/alphagov/accessible-autocomplete#null-options
defaultValue: ""
};
configOptions.onConfirm = function(label) {
if ($selectElem.data('track-category') !== undefined && $selectElem.data('track-action') !== undefined) {
track($selectElem.data('track-category'), $selectElem.data('track-action'), label, $selectElem.data('track-options'));
}
// This is to compensate for the fact that the accessible-autocomplete library will not
// update the hidden select if the onConfirm function is supplied
var value = $selectElem.children("option").filter(function () { return $(this).html() == label; }).val();
$selectElem.val(value).trigger( "change" );
};
new accessibleAutocomplete.enhanceSelectElement(configOptions);
};
It would be great if the default behaviour were kept and this weren't necessary. Alternatively, there could be some kind of option to choose if/when the change was made e.g. changeSelectWithOwnOnConfirmFunction: 'pre|post|off'
Thanks 😄