Typeerror: $(…).autocomplete is not a function

Category: jQuery, Programming | August 7, 2023

Have you ever come across an error message that goes something like “Typeerror: $(…).autocomplete is not a function”? It can be quite a head-scratcher, especially if you’re new to coding. But don’t sweat it – we’ve all been there. This error typically pops up when you’re working with jQuery UI,and it indicates that the autocomplete method isn’t being recognized or found. This might be due to a variety of reasons, like a missing library or a loading sequence issue which can be solved by re-arranging the loading sequence correctly. Let’s dive into this error and find ways to fix it.

What is jQuery UI?

jQuery UI is a comprehensive set of components designed for user interface interactions, effects, widgets, and themes. It is built using the jQuery JavaScript Library. It’s all about making web development fast and easy. Whether you’re building highly interactive web applications or just need to add a date picker to a form control, jQuery UI is the perfect choice. It provides a powerful platform that helps you to design a user-friendly and interactive web interface. Its rich set of features includes autocomplete, drag and drop, modal functionality, and slider among others. Plus, it’s compatible with a wide range of browsers, making your life as a developer a whole lot easier!

What causes that error to occur?

Firstly, this error may occur when the jQuery UI library isn’t correctly linked or simply missing from your project. The autocomplete functionality is a part of the jQuery UI, not the basic jQuery library. Hence, if jQuery UI isn’t correctly integrated, the autocomplete method won’t be available, leading to this error.

Secondly, the loading sequence of your scripts also plays a significant role. The jQuery core library must be loaded before the jQuery UI library because jQuery UI is essentially an extension of the core jQuery library. If the jQuery UI script is loaded first, it won’t find the jQuery object it needs to extend, and thus, you’ll encounter the “Typeerror: $(…).autocomplete is not a function” error.

Tip: When you see this error, it’s basically the script’s way of telling you, “Hey, I can’t find the autocomplete function to use here. Please check if you’ve linked all necessary libraries and the order they’re loaded in.

The scenario of producing that error

Let’s say you have an input element for vegetable names and you are using the autocomplete method from the jQuery UI, so when the user starts to enter characters, the input suggests names that include those characters.

First, you will need to include the scripts for jQuery and jQuery UI, then include the input, and lastly include another script element to use the autocomplete method. So the code for example will be like this:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI</title>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
<div>
<label>Vegetable Name</label>
<input id="vegetableName" type="text">
</div>
<script>
const vegetables = ["Broccoli", "Spinach", "Kale", "Carrots", "Peas", "Bell peppers", "Sweet potatoes", "Garlic", "Onions", "Beetroot"];

$(function() {
$("#vegetableName").autocomplete({
source: vegetables
})
});
</script>
</body>
</html>

All seems fine so far, but when you load this HTML document you will get our “Typeerror: $(…).autocomplete is not a function” error and that’s because of the order of these two lines:

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

The solutions

Solution 1: Load the libraries in the correct order

The order in which you load your libraries matters. The jQuery UI components are built on the jQuery library, so the jQuery library like the parent. Therefore, your HTML script tags should first include the jQuery library ( the parent ) and then the jQuery UI ( the sibling). The scripts are loaded sequentially, so the order should be like:

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

Solution 2:  Check for conflicts

JavaScript libraries, other than jQuery, might use the “$” variable, just like jQuery does. This can cause conflicts with jQuery. By using “jQuery.noConflict()”, you hand back control of the “$” variable to whichever library first implemented it. After you do this, “$” will no longer represent “jQuery”: instead, you’ll need to use the full word “jQuery” to call any jQuery functionality.

Solution 3: Re-initialize the widget

Sometimes, you might have all the scripts properly loaded and in the correct order, but the page itself doesn’t load correctly, or some other script interferes with the initialization of the autocomplete widget. In such a case, re-initializing the widget after the page has fully loaded might help. You can do this by placing your autocomplete initialization code inside a “$(document).ready()” function, which ensures that the entire page (DOM) is fully loaded before the script runs.

Solution 4: Check the jQuery version

The jQuery Autocomplete function is a feature of jQuery UI, not the jQuery core library. Ensure that you’re using a version of jQuery that supports the autocomplete function. For example, if you’re using jQuery 1.3.2, the autocomplete function may not work because it was introduced in jQuery UI 1.8, which requires at least jQuery 1.4.2 to function correctly.

Conclusion

In conclusion, troubleshooting jQuery’s autocomplete function might seem a little daunting at first, but with a systematic approach, you can resolve most issues easily. Remember to check the order of script loadinglook out for conflictscheck the jQuery version, and don’t forget to re-initialize the widget if needed. Keep these tips in your coding toolkit and you’re sure to have a smoother journey navigating the world of jQuery. Happy coding, folks