2

This is what is in my view:

@Html.TextBox("toDateFilter", "", new { @class = "datepicker" })
...etc
...etc


<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>

<script>
    $(document).ready(function () {
        $(".datepicker").datepicker();
    });
</script>

I am getting the error:

Datepicker is not a function.

I have followed the advice of all the other similar questions, and nothing has worked. I also changed the View to these imports instead:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.min.css" />

I also tried this instead:

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

Also I did check that the jquery-ui.min.js file loaded correctly.

Any ideas why this is going wrong? Let me know if I can supply more info...

14
  • 2
    Suggests to me that your .ready() is running before the jquery-ui.min script has loaded. Can you put a breakpoint on that line and see if the external script has loaded at point? Commented Nov 17, 2015 at 16:19
  • 1
    Your code should work fine. I just copied and pasted it from your question and it worked fine for me. Commented Nov 17, 2015 at 16:20
  • 1
    @AlanSchapira - at what point in the code does it run that test? Commented Nov 17, 2015 at 16:36
  • 1
    it works perfectly: jsfiddle.net/u52pvn3j Commented Nov 17, 2015 at 16:42
  • 1
    I would make sure external scripts are in the <head> and also maybe look into having the scripts load from your own domain, rather than CDN. It should then be safe to expect that ready() will run when all dependencies have loaded Commented Nov 17, 2015 at 16:51

3 Answers 3

6

Datepicker is not a function. i am also facing this issue.i have resolved it using Jquery Noconflict function.

$.noConflict();
jQuery(document).ready(function ($) {
    $(function () {
        $('.datefield').datepicker();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

This is the answer from Starscream1984 in the comments, so props to them!

The issue is that the ready() function was being called before the jquery-ui.js script had finished loading.

This could not be fixed by moving it into the <head> nor loading the scripts from my own domain.

I was able to fix it simply by removing the ready() function entirely, and just running the contents.

If anyone knows how I can force the script to load first, or the ready() function to be called last, please let me know.

Comments

0

Have you tried doing something like this:

$(function () {
    $("#nameOfTheFieldInModel").datepicker({
        dateFormat: 'dd-mm-yy',
        changeMonth: true,
        changeYear: true
    });
});

In document ready?

2 Comments

If datepicker() is not a function, it doesn't matter what you try to pass in to it.
@AlanSchapira you could try moving the jquery inclusion in the Site.Master

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.