Good afternoon guys.
I'm trying to implement an autocomplete using typeahead.js. Assign a change event to my input to make an ajax request that brings me the data that will be displayed in the list.
However, when the screen loads and I type something my event is not triggered. If I take the focus off the component and re-enter the event is not triggered either. It is fired only when I click on the address bar, or mute the tab in the browser or window. After the event is triggered for the first time, every time I type something the event is fired normally. Any idea why this might be happening?
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
$(function(){
$("#nome").change(function(){
var input = $(this).val()
$.ajax({
url: "loadNameChoices",
type: "get",
data:{input:input},
contentType: 'application/json',
success: function(response) {
$("#nome").typeahead({source:response});
},
});
});
});
</script>
</head>
<body>
<div class="container-fluid">
<div class="panel panel-primary">
<g:form name="formBusca" class="form-inline">
<div class="panel-body">
<div class="form-group">
<label>Nome:</label>
<g:textField id="nome" name="nome" class="form-control" autocomplete="off"/>
</div>
<div class="form-group">
<label>Usuário:</label>
<g:textField id="username" name="username" class="form-control" autocomplete="off"/>
</div>
</div>
<div class="panel-footer">
<div class="text-right">
<button type="button" class="btn btn-default btn-sm" id="search-button">Pesquisar</button>
<g:link action="form" class="btn btn-default btn-sm">Novo</g:link>
</div>
</div>
</g:form>
</div>
</div>
</body>
</html>
The application's backend is failing with Grails, but I do not think it has anything to do with it.
Thank you.