I'm currently working on implementing the autocomplete feature following a tutorial, and while it's functioning, I'm facing an issue with submitting the form when the user selects an item and hits enter. Below is the Coffeescript code that I'm using, but for some reason, it doesn't work as expected. Can someone point out where I might be going wrong?
jQuery ->
$('#search_bar').autocomplete(
source: $('#search_bar').data('autocomplete-source')
).keydown (e) ->
$('#search_button').trigger "submit" if e.keyCode is 13
My goal is to also have the form submit upon selecting an item with a mouse click – not sure if this is achievable though?
Update: Attempted the following...
jQuery ->
$('#search_bar').autocomplete
source: $('#search_bar').data('autocomplete-source'),
select: (event, ui) ->
$(this).parents("form").submit()
While this method now works if you use the keyboard to select an item and press enter, selecting an item with a mouse click only sends the typed string, not the complete word from the auto-complete drop-down. Perhaps the search field needs to be updated with the text content on mouse-over?
Update 2: Problem solved! Just include the following at the end
focus: (event, ui) ->
$('#search_bar').val(ui.item.value)