TypeError: $(...).draggable is not a function in jQuery

avatar

Borislav Hadzhiev

Last updated: Oct 19, 2021

banner

Photo from Unsplash

TypeError: $(...).draggable is not a function in jQuery #

The "$(...).draggable is not a function" jQuery error occurs for multiple reasons:

  1. Forgetting to include the jQuery UI library.
  2. Loading the jQuery UI library before the jQuery library.
  3. Loading the jQuery library twice.
  4. Specifying an incorrect path to the jQuery files.

jquery draggable is not a function error

To solve the "$(...).draggable is not a function" jQuery error, make sure to load the jQuery library before loading the jQuery UI library. The libraries have to be loaded only once on the page, otherwise the error is thrown.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <!-- ✅ Load CSS file for jQuery UI --> <link href="https://code.jquery.com/ui/1.12.1/themes/ui-lightness/jquery-ui.css" rel="stylesheet" /> <style> #draggable { width: 150px; height: 150px; padding: 0.5em; } </style> </head> <body> <div id="draggable" class="ui-widget-content"> <p>Drag me around</p> </div> <!-- ✅ load jQuery ✅ --> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous" ></script> <!-- ✅ load jQuery UI ✅ --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha512-uto9mlQzrs59VwILcLiRYeLKPPbS/bT71da/OEBYEwcdNUk8jYIy+D176RYoop1Da+f9mvkYrmj5MCLZWEtQuA==" crossorigin="anonymous" referrerpolicy="no-referrer" ></script> <!-- ✅ run your JS script ✅ --> <script src="index.js"></script> </body> </html>

The order we loaded the scripts in is very important. The order is the following:

  1. Load the CSS for the jQuery UI library.
  2. Load the jQuery library.
  3. Load the jQuery UI library.
  4. Run your JS file (index.js in the example).

Here's the code for the index.js file.

index.js
$(document).ready(function () { $('#draggable').draggable(); });

If you open your browser you will see the draggable div.

jquery draggable

If you are still getting the error, make sure you're loading the jQuery UI library after loading jQuery and you're not loading either library twice.

Loading the jQuery library twice re-runs the initialization process and causes the error.

When loading the libraries from files on your local files system, make sure that the paths you specify are correct and point to the right files.

Specifying an incorrect path to the jQuery or jQuery UI scripts is equivalent to not loading the scripts at all.

You can check if the specified paths are correct by opening the console in your browser and checking if you have any 404 errors related to loading the jQuery or jQuery UI libraries.

Conclusion #

To solve the "$(...).draggable is not a function" jQuery error, make sure to load the jQuery library before loading the jQuery UI library. The libraries have to be loaded only once on the page, otherwise the error is thrown.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.