Resolving Uncaught TypeError: $(...).tooltip is not a function: Analysis of jQuery Plugin Loading Order and Conflicts

Dec 07, 2025 · Programming · 10 views · 7.8

Keywords: jQuery | JavaScript error | plugin loading order | Spring MVC | front-end debugging

Abstract: This article delves into the common JavaScript error 'Uncaught TypeError: $(...).tooltip is not a function' in Spring MVC projects, exploring its root causes and solutions. Through a detailed case study, it explains jQuery plugin dependencies, the importance of script loading order, and $ symbol conflicts. The article first reproduces the error scenario with JSP code loading multiple CSS and JavaScript files, then systematically presents three solutions: reordering script loads, using jQuery instead of $, and checking version compatibility. Each solution includes code examples and technical explanations to help developers understand the underlying mechanisms. It also covers debugging with browser developer tools and provides best practices for prevention, such as using modular loading tools and version management strategies.

Error Scenario Reproduction and Analysis

In projects based on the Spring Web MVC framework (version 3.2.8) deployed on WebLogic Server 12.1.2.0.0, developers often encounter a typical JavaScript error: Uncaught TypeError: $(...).tooltip is not a function. This error usually occurs when loading JSP pages, indicating that the jQuery tooltip method cannot be invoked correctly. Below is a typical error code example showing resource loading order:

<link href="/tdk/styles/bootstrap.css" type="text/css" rel="stylesheet">
<link href="/tdk/styles/admin.css" type="text/css" rel="stylesheet">
<link href="/tdk/styles/jquery.dataTables.css" type="text/css" rel="stylesheet">
<link href="/tdk/styles/dataTables.bootstrap.css" type="text/css" rel="stylesheet">
<link href="/tdk/styles/slides.css" type="text/css" rel="stylesheet">

<script src="/tdk/scripts/jquery.min.js" type="text/javascript"></script>
<script src="/tdk/scripts/bootstrap.js" type="text/javascript"></script>
<script src="/tdk/scripts/jquery.dataTables.js" type="text/javascript"></script>
<script src="/tdk/scripts/dataTables.bootstrap.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js" type="text/javascript"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
    $('[data-toggle="tooltip"]').tooltip();
    // Other code...
  });
</script>

In this example, the error occurs when calling $('[data-toggle="tooltip"]').tooltip() inside the $(document).ready function. Although all script files appear loaded, the tooltip function is undefined, causing a type error. This often stems from two core issues: improper script loading order or $ symbol conflicts.

Solution 1: Optimizing Script Loading Order

jQuery plugins depend on the jQuery core library and potentially other libraries (e.g., jQuery UI). If a plugin loads before its dependencies, its functionality may not initialize properly. In the above code, bootstrap.js might include the tooltip plugin, but it loads before jQuery UI, and jQuery UI version 1.12.1 could override or conflict. Reordering scripts can resolve this:

<script src="/tdk/scripts/jquery.min.js" type="text/javascript"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="/tdk/scripts/bootstrap.js" type="text/javascript"></script>
<script src="/tdk/scripts/jquery.dataTables.js" type="text/javascript"></script>
<script src="/tdk/scripts/dataTables.bootstrap.js" type="text/javascript"></script>

This order ensures: jQuery core loads first, then jQuery UI (providing tooltip functionality), followed by Bootstrap (which may depend on jQuery UI), and finally DataTables plugins. This satisfies all dependencies, so the tooltip function is defined when called.

Solution 2: Handling $ Symbol Conflicts

In some cases, multiple JavaScript libraries might redefine the $ symbol, causing jQuery functions to fail. For example, if other scripts use $ for different purposes, calling $(...).tooltip() will fail. Using jQuery instead of $ avoids such conflicts:

<script type="text/javascript">
  jQuery(document).ready(function($) {
    $('[data-toggle="tooltip"]').tooltip();
    // Use $ as a local variable to ensure it points to jQuery
  });
</script>

Or, use jQuery globally:

<script type="text/javascript">
  jQuery(document).ready(function() {
    jQuery('[data-toggle="tooltip"]').tooltip();
  });
</script>

This approach explicitly references the jQuery object, bypassing potential $ symbol conflicts and ensuring code stability.

Solution 3: Checking Version Compatibility and Debugging Tips

Beyond loading order and symbol conflicts, version incompatibility can also cause the tooltip function to be undefined. For instance, if Bootstrap version mismatches with jQuery UI, plugin functionality may fail. Using browser developer tools (e.g., Chrome DevTools) to inspect network requests and error logs helps identify issues:

Additionally, consider using modular tools like Webpack or RequireJS to manage dependencies and automatically handle loading order. In Spring MVC projects, optimize static resource loading by configuring resource handlers.

Best Practices and Conclusion

Preventing the Uncaught TypeError: $(...).tooltip is not a function error hinges on following clear dependency management strategies:

  1. Standardize Script Order: Always load jQuery core first, then dependencies (e.g., jQuery UI), and finally plugins (e.g., Bootstrap, DataTables).
  2. Avoid $ Conflicts: In complex projects, use jQuery instead of $, or pass $ as a parameter in ready functions.
  3. Version Control: Ensure all library versions are compatible, update regularly, and test thoroughly.
  4. Error Handling: Add conditional checks, such as if (typeof $.fn.tooltip !== 'undefined') { ... }, to enhance code robustness.

By understanding these principles, developers can quickly diagnose and resolve similar errors, improving web application reliability and user experience. In frameworks like Spring MVC, combining server-side configuration with client-side optimization effectively reduces front-end errors.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.