Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: $(...).datepicker is not a function(anonymous function)

I found few answers on stack overflow but still cant resolve my problem. I am running on Django but I dont think it is relevant for this error.

I try to make work my date picker java script but I am getting the error

1:27 Uncaught TypeError: $(...).datepicker is not a function(anonymous function) @ 1:27fire @ jquery-1.9.1.js:1037self.fireWith @ jquery-1.9.1.js:1148jQuery.extend.ready @ jquery-1.9.1.js:433completed @ jquery-1.9.1.js:103 jquery-2.1.0.min.js:4 XHR finished loading: POST "https://localhost:26143/skypectoc/v1/pnr/parse".l.cors.a.crossDomain.send @ jquery-2.1.0.min.js:4o.extend.ajax @ jquery-2.1.0.min.js:4PNR.findNumbers @ pnr.js:43parseContent @ contentscript.js:385processMutatedElements @ contentscript.js:322

This is all my scripts :

<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />  <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>  <script type="text/javascript">     $(document).ready(function() {         $('.dateinput').datepicker({ format: "yyyy/mm/dd" });     });  </script>  <!-- Bootstrap core JavaScript ================================================== -->  <!-- Placed at the end of the document so the pages load faster --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>  <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script> <script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script> <!-- Just to make our placeholder images work. Don't actually copy the next line! --> <script src="http://getbootstrap.com/assets/js/vendor/holder.min.js"></script> <!-- IE10 viewport hack for Surface/desktop Windows 8 bug --> <script src="http://getbootstrap.com/assets/js/ie10-viewport-bug-workaround.js"></script> <script type="text/javascript">     $(document).ready(function() {         $("#extra-content").hide();         $("#toggle-content").click(function(){             $("#extra-content").toggle();         });     }); </script>             

any feedback will be very appreciated

like image 753
Ilya Bibik Avatar asked Mar 24 '16 18:03

Ilya Bibik


People also ask

What is Datepicker in JavaScript?

The JavaScript DatePicker (Calendar Picker) is a lightweight and mobile-friendly control that allows end users to enter or select a date value. It has month, year, and decade view options to quickly navigate to the desired date.

Is not a function Typeerror is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

Is DateTimePicker not a function error?

But there would always be a ... datetimepicker is not a function error. What worked for me was bringing in the .js and .css files individually.

Why does $ (...) datepicker become undefined?

It undoes the plugging in and therefore $ (...).datepicker becomes undefined. Although the first $ (document).ready block appears before that, the anonymous callback function body is not executed until all scripts are loaded, and by then $ (...) ( window.$ to be precise) is referring to the most recently loaded jQuery.

How to add datepicker to jQuery-UI?

If you want to use datepicker from jquery-ui, it would probably make most sense to include the jquery-ui script after bootstrap. jquery-ui 1.11.4 is compatible with jquery 1.6+ so it will work fine. Alternatively (in particular if you are not using jquery-ui for anything else), you could try bootstrap-datepicker.

Is it possible to call datepicker immediately rather than in document ready?

You would not run into this if you called $ ('.dateinput').datepicker immediately rather than in $ (document).ready callback, but then you'd need to make sure that the target element (with class dateinput) is already in the document before the script, and it's generally advised to use the ready callback.


2 Answers

What went wrong?

When you include jQuery the first time:

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

The second script plugs itself into jQuery, and "adds" $(...).datepicker.

But then you are including jQuery once again:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> 

It undoes the plugging in and therefore $(...).datepicker becomes undefined.

Although the first $(document).ready block appears before that, the anonymous callback function body is not executed until all scripts are loaded, and by then $(...) (window.$ to be precise) is referring to the most recently loaded jQuery.

You would not run into this if you called $('.dateinput').datepicker immediately rather than in $(document).ready callback, but then you'd need to make sure that the target element (with class dateinput) is already in the document before the script, and it's generally advised to use the ready callback.

Solution

If you want to use datepicker from jquery-ui, it would probably make most sense to include the jquery-ui script after bootstrap. jquery-ui 1.11.4 is compatible with jquery 1.6+ so it will work fine.

Alternatively (in particular if you are not using jquery-ui for anything else), you could try bootstrap-datepicker.

like image 155
szym Avatar answered Sep 22 '22 13:09

szym


The error is because you are including the script links at two places which will do the override and re-initialization of date-picker

<meta charset="utf-8">  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />      <script src="http://code.jquery.com/jquery-1.9.1.js"></script>  <script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>    <script type="text/javascript">  $(document).ready(function() {      $('.dateinput').datepicker({ format: "yyyy/mm/dd" });  });   </script>          <!-- Bootstrap core JavaScript  ================================================== -->  <!-- Placed at the end of the document so the pages load faster -->  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

So exclude either src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"

or src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"

It will work..

like image 40
anvesh Avatar answered Sep 24 '22 13:09

anvesh