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

83
Views
typeahed perform ajax call only if string contains space

I'm trying to use/configure typeahead to just perform the ajax call if the typed string contains space. To be more specific, I would like to display fulladdresses but at my API I would like to avoid the street number part. I was trying to achieve that using a function assigned to the 'source' property:

   $("#select").typeahead({
            minLength: 3,
            source: function (request, response) {
                var streetName = '';

            for (i = 1; i < streetParts.length; i++) {
                streetName += streetParts[i] + ' ';
            }

            if (streetName.length < 3) return null;

            $.ajax({
                url: "/api/Search/suggest/",
                data: { "input": streetName.trim() },
                type: "GET",
                hint: true,
                contentType: "json",
                success: function (data) {
                    items = [];
                    map = {};
                    $.each(data, function (i, item) {
                        var id = item.document.id;
                        var name = item.text;
                        map[name] = { id: id, name: name };
                        items.push(name);
                    });
                    response(items);
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        updater: function (item) {
            //If simultaneously want to update value somewhere else
            $("#selected").html(map[item].id);
            return item;
        }
    });

even though I can see the request at the network tab is fine, the div with the results are not being rendered. If I remove the if statement from the source function it works fine. Is it the right way to cancel the execution of the source function (returning null)? Is there any other property or way to achieve what I'm trying?

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!