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

204
Views
How can you return an explicit "No results found" value to select2 ajax search?

I am using the select2 jQuery dropdown box plugin (latest version), and have an issue where I'm allowing the user to enter an Employee ID to return the names of matching employee(s). This works.

In all searches, I return a value (the "status" of the ajax call, and the employee array if found). I suspect this may be confusing select2 into always thinking there are results.

When there are no actual employees returned, how can I explicitly tell select2 that no results were found so it displays that message?

Currently, I get a nasty jQuery error when no results are found:

jquery-3.6.0.min.js:2 jQuery.Deferred exception: Cannot read properties of undefined (reading 'length') TypeError: Cannot read properties of undefined (reading 'length')
    at e (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js:2:39169)
    at Object.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js:2:38243)
    at e (https://code.jquery.com/jquery-3.6.0.min.js:2:30038)
    at t (https://code.jquery.com/jquery-3.6.0.min.js:2:30340) undefined
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Was going to delete as I found the answer shortly after, but figured this might help future coders out.

Simply return an explicit "results" value with an empty json object in the format select2 expects like below:

return { results: [] };
about 4 years ago · Juan Pablo Isaza Report
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!