Correct way to set autocomplete value


Topic: Correct way to set autocomplete value

Watts asked 2 years ago

Expected behavior

What's the correct way to dynamically set an autocomplete value from jquery. The following command:

$('#form-autocomplete').val('Massachusetts')

See Snippet - It does not set the field value as if it was selected by a user. The label overlaps and the cross button doesn't show.

P.S. This would be good to add to documentation.

Thanks

Actual behavior

See snippet

Resources (screenshots, code snippets etc.)

https://mdbootstrap.com/snippets/jquery/watts/1904750


Mateusz Łubianka staff answered 2 years ago

Hi @Watts,

Try to use this code:

$('#form-autocomplete').val('Wisc').trigger('keyup').trigger('focus'); 

Best,


Watts commented 2 years ago

Thanks!

The final step was:

$('.mdb-autocomplete-wrap li').trigger('click')


Mateusz Łubianka staff commented 2 years ago

I'm glad I could help.

Best,


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: 4.14.1
  • Device: DESKTOP
  • Browser: Chrome
  • OS: Linux
  • Provided sample code: No
  • Provided link: Yes