150

When i use DatePicker, jQuery's UI plugin, in an existing .aspx page I get errors that:

$("#datepicker").datepicker is not a function

However, when I copy and paste the same code that creates and uses the datePicker to an HTML file that's also in the same directory as the aspx page, it works flawlessly. This leads me to assume that there are some JS files in the aspx page that's preventing the datePicker or maybe jQuery's UI JS files to load properly.

Can anyone confirm my beliefs or provide any tips on finding the culprit that's interfering with jQuery's UI plugins?

kamaci
  • 72,915
  • 69
  • 228
  • 366
burnt1ce
  • 14,387
  • 33
  • 102
  • 162
  • Can you post the relevant jQuery code please – Russ Cam Jul 31 '09 at 14:03
  • How are you including the datepicker plugin on the page - is it in a ScriptManagerProxy or are you writing it directly into the page? Are you sure that it is being loaded? If so, what other plugins are you including? Are you sure you've included ui.core.js? – James Kolpack Jul 31 '09 at 14:07
  • 12
    I found the problem. I wish i have found this solution yesterday as oppose to an 1hr after posting this question. The JS code that I've written references jQuery and jQuery UI javascript file acted as a module. Its parent also references jQuery at the bottom of the body tag (so 2 references to jQuery). Since jQuery is reinitialized after jQuery UI, jQuery UI is wiped out as a plugin, hence why my code could not find the DatePicker plugin. – burnt1ce Jul 31 '09 at 15:16
  • Since I'm using Webpack, I needed to use jquery-ui-bundle instead. https://stackoverflow.com/a/39230057/470749 – Ryan Nov 16 '18 at 19:49

23 Answers23

228

I struggled with a similar problem for hours. It then turned out that jQuery was included twice, once by the program that I was adding a jQuery function to and once by our in-house debugger.

Eugene van der Merwe
  • 4,390
  • 1
  • 35
  • 48
  • 4
    This was my issue too. I had a reference to my own jQuery library along with jQuery Tools CDN which unknowingly included jQuery. – DavGarcia Apr 23 '13 at 19:47
  • This was also my issue. I'm using superfish and it was also loading jquery. I didn't notice it until now. – rrtx2000 Jan 05 '14 at 07:13
  • 57
    So what the solution? – Henrique Ordine Mar 16 '15 at 12:29
  • 8
    @HenriqueOrdine double check that jQuery is not included twice on the page. – Eugene van der Merwe Mar 16 '15 at 15:58
  • This was also my problem. I was using common assets within the rails asset pipeline and was both declaring jquery in my top-level meta file (application.js) AND had a jquery min file in my JS folder. Double-declare. I just removed the jquery declaration. – Mario Jun 09 '15 at 18:27
  • The answer for me too. Was included via ActiveAdmin in my case. – Rob Olendorf Aug 23 '15 at 20:44
  • If you import certain versions of DataTables, it can also give you this problem because it includes jquery. – Marcel Gruber Sep 01 '15 at 23:03
  • My page does have jquery four times, but three of them are added by chrome extension, will it still create problem. if yes, then even if we design the page properly if that page is loaded by client who is using extensions which adds jquery, then also the page will not work properly – Akshay Vijay Jain Dec 12 '16 at 10:51
  • Make sure you have the right target set, eg: `jQuery(".date-field").datepicker()` instead of `jQuery(".datefield").datepicker()`, as wrong target may trigger `TypeError` issue. Just wanted to share as I had the same issue and it was nothing with the multiple jQuery version. – Anunay Dec 14 '19 at 08:55
49

If there is another library that is using the $ variable, you can do this:

var $j = jQuery.noConflict();
$j("#datepicker").datepicker();

Also make sure your javascript includes are in the correct order so the jquery core library is defined before the jquery.ui. I've had that cause issues.

MacAnthony
  • 4,471
  • 2
  • 23
  • 26
31

jQuery “ $(”#datepicker“).datepicker is not a function”

I have fixed the issue by placing the below three files in the body section of the form at the end.

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="Stylesheet" type="text/css" />
Krishna
  • 7,290
  • 3
  • 25
  • 25
26

This error usually appears when you're missing a file from the jQuery UI set.

Double-check that you have all the files, the jQuery UI files as well as the CSS and images, and that they're in the correctly linked file/directory location on your server.

random
  • 9,774
  • 10
  • 66
  • 83
  • This answer is closest to the solution that i've posted. – burnt1ce Jul 31 '09 at 15:17
  • Note to self: make sure that in switching from local files to google's CDN files, that you actually have the right url. It's ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js, not ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery-ui.min.js, dang it. – neminem Aug 21 '13 at 15:31
9

If you think that there is a conflict you can use jQuery.noConflict() in your code. Details are in the docs.

REFERENCING MAGIC - SHORTCUTS FOR JQUERY

If you don't like typing the full "jQuery" all the time, there are some alternative shortcuts:

Reassign jQuery to another shortcut var $j = jQuery; (This might be the best approach if you wish to use different libraries) Use the following technique, which allows you to use $ inside of a block of code without permanently overwriting $:

(function($) { /* some code that uses $ */ })(jQuery)

Note: If you use this technique, you will not be able to use Prototype methods inside this capsuled function that expect $ to be Prototype's $, so you're making a choice to use only jQuery in that block. Use the argument to the DOM ready event:

jQuery(function($) { /*some code that uses $ */ });

Note: Again, inside that block you can't use Prototype methods

Thats from the end of the docs and might be useful to you

AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
8

Go for the obvious first: Are you referencing well the jquery-ui.js file?

Try using the network tab of firebug to find if it is loaded, or the Information\View javascript code of the Web Developer Toolbar.

eKek0
  • 23,005
  • 25
  • 91
  • 119
3

Have you tried using Firebug to 1) determine that there are no Javascript errors and 2) that the #datepicker element exists on the page?

Most likely there is an error prior to the datepicker call that is preventing the datepicker call from executing.

Kevin Griffin
  • 2,239
  • 4
  • 25
  • 27
3

I know that this post is quite old, but for reference I fixed this issue by updating the version of datepicker

It is worth trying that too to avoid hours of debugging.

https://cdnjs.com/libraries/bootstrap-datepicker

Yannickv
  • 527
  • 4
  • 13
  • 3
    This is not applicable to Jquery UI datepicker. Bootstrap datepicker is a whole different animal. – RedSands Oct 10 '19 at 23:30
2

I just had this issue, and moving the JS references and code towards the bottom of the page before the </body> tag fixed it for me.

Banjer
  • 8,118
  • 5
  • 46
  • 61
1

Also check the permissions for the directory you download the files into. For some reason when I downloaded this, by default it was saved in a folder with no access allowed! It took forever to figure out that was the problem, but I just had to change the permission for the directory and it's contents - setting it so EVERYONE = READ ONLY. Works great now.

1

I ran into this problem recently - the issue was that I had included the jQuery UI files in the head tag, but the Telerik library I was using included jQuery at the bottom of the body tag (thus apparently re-initializing jQuery and unloading the UI plugins previously loaded).

The solution was to find out where jQuery was actually being included, and including the jQuery UI scripts after that.

1

For me it was just necessary to remove the "defer" property from the declaration of my script

user3799115
  • 123
  • 1
  • 9
0

Some file no load before you call.

For example: Your code:

<%= javascript_include_tag "distpicker.min" %>
<%= javascript_include_tag "distpicker.data.min" %>

Change to this:

<%= javascript_include_tag "distpicker.data.min" %>
<%= javascript_include_tag "distpicker.min" %>
  • This would be a specific ruby solution. Not necessarily conclusive to the original question. – tblev Jun 28 '21 at 20:16
0

I could fix this problem removing the jquery bundle on the _Layout.cshtml

Header

    <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script src="~/Scripts/kendo/2015.2.902/kendo.all.min.js"></script>
    ...

Footer

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)

Change footer to

    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
jaimenino
  • 64
  • 3
0

Have you tried using $("#<%= txtDateElementId.ClientID %>").datepicker(); instead of $("#txtDateElementId").datepicker(); when selecting an element by ID using JQuery.

Emil Filip
  • 221
  • 3
  • 11
0

I just ran into a similar issue. When I changed my script reference from self-closing tags (ie, <script src=".." />) to empty nodes (ie, <script src=".."></script>) my errors went away and I could suddenly reference the jQuery UI functions.

At the time, I didn't realize this was just a brain-fart of me not closing it properly to begin with. (I'm posting this simply on the chance that anyone else coming across the thread is having a similar issue.)

Pang
  • 9,564
  • 146
  • 81
  • 122
Peter Bernier
  • 8,038
  • 6
  • 38
  • 53
0

Old question - newer version of .NET (4.5.6) - same issue - new answer

Using NuGet

Make sure you have the following installed via NuGet:

  • jQuery
  • AspNet.ScriptManager.jQuery
  • jQuery.UI.Combined
  • AspNet.ScriptManager.jQuery.UI.Combined

Install them from here [ Tools > NuGet Package Manager > Manage NuGet Packages for Solution ]

Then, jquery and jquery.ui.combined should be added to your ScriptManager in Site.Master. Will look something like this:

    <asp:ScriptManager runat="server">
        <Scripts>
            <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
            <%--Framework Scripts--%>
            <asp:ScriptReference Name="MsAjaxBundle" />
            <asp:ScriptReference Name="jquery" />
            <asp:ScriptReference Name="jquery.ui.combined" />
            <asp:ScriptReference Name="bootstrap" />
            <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
            <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
            <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
            <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
            <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
            <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
            <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
            <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
            <asp:ScriptReference Name="WebFormsBundle" />
            <%--Site Scripts--%>
        </Scripts>
    </asp:ScriptManager>

This solved my same issue. Thank you.

Taylor Brown
  • 1,689
  • 2
  • 17
  • 33
0

(”#datepicker“).datepicker is not a function

I also ran into this problem a few days ago. It occurs probably because of having the jquery link more than once in the file. Try searching the whole file for that duplicate content.

It will look somewhat like this:

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

Keep one in the <head></head> tag and remove all the rest. This will fix the issue.

JOSE C S
  • 31
  • 9
0

For those using Laravel, the default app.js has a defer in its declaration.
DELETE IT

enter image description here

Lefty
  • 1,192
  • 13
  • 18
0

referencing jQuery twice might cuase the issue.

user123456
  • 2,524
  • 7
  • 30
  • 57
0

Your code needs to include JQuery UI, paste this after your jQuery script:

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

If you want higher security then you can download version 1.13 on your PC, as 1.12 has some vulnerabities and Google doesn't host the newest version yet.

DGX37
  • 304
  • 3
  • 12
0

For me the error was happening because of invalid date format:

Invalid code:

<script>
    $('#targetDate').datepicker({
        format : 'dd/MM/yyyy'
    });
</script>

Valid code:

<script>
    $('#targetDate').datepicker({
        format : 'dd/mm/yyyy'
    });
</script>

Notice the MM. Please see if the format of your date is correct. Hope it helps!

-1

In my case, it was solved by changing the import order of the following scripts: Before (Not work):

After (Working):

Matías W.
  • 350
  • 2
  • 17