Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Bootstrap Typeahead select

Hi all,

I can't seem to get my code to work so when a user selects from the autocomplete it enters into the input.

Heres the code:

// JS

let path = "{{ url('search/landlord') }}";

$('#search').typeahead({
    minLength: 3,
    source:  function (query, process, ) {
        return $.get(path, { query: query }, function (data) {
            return process(data);
        });
    },
    select: function( ui, item ) {
        console.log(item); 
    }
});


// HTML

<input type="text" class="form-control" name="term" id="search" placeholder="Start typing something to search..." data-provide="typeahead" autocomplete="off">


So things like ui.item.value & ui.item.name also show undefined.

Both console.log(item); and console.log(ui); shows as undefined... Any suggestions on whats wrong or what I may have missed.

0 likes
5 replies
theUnforgiven's avatar

Anyone else encountered this problem and how did you fix?

patrick591's avatar

Possible solution (different library): https://github.com/devbridge/jQuery-Autocomplete

<script src="/js/autocomplete/jquery.autocomplete.min.js"></script>
<script>

$('#quick_search_name').devbridgeAutocomplete({
    serviceUrl: '<?php echo route('search.contact');?>',
    orientation: 'top',
    onSelect: function (suggestion) {
        
        globalContactCard(suggestion);
        $('#quick_search_name').val('');
    }

});
</script>
theUnforgiven's avatar

Will have a look at this thanks, but surely $('#quick_search_name').val(''); would just get the value from what is typed rather than what is shown from the autocomplete?

patrick591's avatar

Actually that just blanks out the input field after they click on the value they want. The data is in the suggestion variable.. i.e. you could do console.log(suggestion.fieldname); i.e. JSON coming back from the request.

Please or to participate in this conversation.