Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
How do I use Jquery UI autocomplete with dynamically fetched json object triggered by change event

First, I am trying to trigger the json object fetch on change event. Then use that json object in the autocomplete feature. This is the code I have written so far, the problem I am facing is that the json object only gets fetched after I click out of the text box. Also the json object I receive is an array of array with two values which I flattened out using flatmap(). I know I am trying to do a lot at once, but I think this should be possible. This is what I have so far.

    $(document).ready(function() {
    $( "#username" ).bind('change',function() {
        var userVal = $( this ).val();
        var dict = [];
        url= '/someurl/json_service?q=' + userVal;
        $.ajax({
        url: url,
        method: 'GET',
        dataType: 'json',
        success: function(data) {
            const output = data.flatMap(a => a.map(b => b[0]));
            $.each(output, function() {
            dict.push(this.toString());
            });
            return dict;
        }
        });
        console.log(dict);
        $('#username').autocomplete({
            source: dict
        });
    });
    $("#username").trigger('change');
});

And the html is as follows

   <tr>
     <th class='bb' align='right'>Buyer Username:</th>
     <td class='bb  ui-widget'><input type='text' name='username' value='[username]' id="username"> &nbsp;<a href="/website/users" target="B_BLANK">Find User &raquo;</a></td>
    </tr>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!