TypeError: $(...).daterangepicker is not a function
When debugging date pickers in the browser, the console error TypeError: $(...).daterangepicker is not a function is a classic symptom of a script loading order issue. It rarely indicates a bug in the plugin itself; instead, it signals that the jQuery UI component has been called before the plugin's JavaScript file was fully parsed and attached to the global object.
Looking at the provided code snippet, the root cause becomes immediately apparent in the <head> section. The script tags are introducing duplicate instances of jQuery and Bootstrap.
First, you have a standard Google CDN call:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Immediately following that is another script tag pulling jQuery from cdn.jsdelivr.net:
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
While modern browsers are generally forgiving regarding redundant library loads, this specific setup creates a timing vulnerability for plugins like bootstrap-daterangepicker. This plugin relies on the global $ or jQuery variable to register its methods. If the first script tag loads successfully but is interrupted (due to network latency, ad blockers, or a slow server response), or if the second script overwrites the context in an unexpected way, the plugin initialization fails when your custom code tries to access $.daterangepicker.
The most reliable fix is to consolidate these dependencies. Remove one of the jQuery instances and ensure there are no conflicting Bootstrap versions interfering with the DOM manipulation required by the picker.
Correcting the Dependency Chain
To resolve the "not a function" error, you must ensure a strict loading order:
- jQuery: Load this first. No duplicates.
- Moment.js: The date range picker depends on Moment.js for time calculations. This must load before the plugin.
- Bootstrap: If using Bootstrap utilities, load its JS after jQuery but ensure it doesn't conflict with custom scripts.
- Bootstrap Date Range Picker: This is the critical step. It must load after all dependencies are ready.
Here is a cleaned-up version of your head section:
<!-- Dependencies -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Date Range Picker Prerequisites -->
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css">
<!-- Plugin Core -->
<script src="https://cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<!-- Custom Styles and Logic -->
<link rel="stylesheet" href="{{ asset('ui/css/custom.css') }}">
<script>
$(function() {
var start = moment().subtract(29, 'days');
var end = moment();
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start, end);
});
</script>
The DOM Timing Problem
Even with the correct files loaded, this error can persist if your JavaScript block runs before the HTML element #reportrange exists in the DOM. This is a common pitfall when placing initialization code inside <head> tags or at the top of the body without ensuring the container is ready.
The solution provided above uses $(function() { ... }), which is jQuery's shorthand for .ready(). This ensures that the callback function only executes after the DOM structure is fully constructed, guaranteeing that #reportrange exists before you attempt to attach the plugin to it.
PHP Integration Considerations
In your PHP file, you are rendering a standard Bootstrap button:
<div id="reportrange" class="pull-right" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%">
<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
<span></span> <b class="caret"></b>
</div>
This markup is correct for the plugin. However, if you are using Laravel's Blade syntax (as suggested by {{ asset() }} in your custom CSS link), ensure that the view rendering happens after the layout files containing the scripts have been processed. Sometimes, caching issues or incorrect include orders in Blade templates can result in scripts appearing to load out of sequence relative to the actual DOM render.
If you continue to face this issue despite correcting the script order, consider moving your initialization logic to a separate JavaScript file included at the very bottom of your <body> tag. This guarantees that both the DOM and all external resources are fully loaded before execution begins.
For more robust date handling patterns in Laravel projects, exploring community resources or dedicated packages can simplify these dependency chains further.