🛠️ Typeahead: How to add Ajax

Hey ,

I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.

Please upgrade (use this link) access this content.

I'm super eager to help you learn more!

🛠️ Typeahead: How to add Ajax

You can get data for the Typeahead via Ajax in two ways:

  1. Fetch all data before activating the Typeahead
  2. Fetch data as user types into the input field

When to use Method 1

You want to use method 1 if you have a small amount of data to fetch. If you have less data, users need to wait a lesser amount of time before they use the Typeahead.

Also, you won’t have to worry about debouncing, optimising performance, and all the complicated ajax stuff.

When to choose Method 2

You only want to use Method 2 if:

  1. You have a large amount of data (too much to download).
  2. You cannot predict what users will type.

A good example here is Google. You can’t predict what users would search for. Trying to download the entire search index is going to be way too expensive for users.

Other good examples include search results.

What we’ll use

Since the Typeahead requires a small amount of data, we’ll use the first method.