javascripthtmljquerydatetimepicker

how to set up datetimepicker


I can't seem to set this jquery datetimepicker. I believe I am following the instructions correctly. However, only the input box displays. After inspecting I saw these errors in the log

enter image description here

This the plugin I am trying to use.

https://xdsoft.net/jqplugins/datetimepicker/

previous code

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Home</title>
  <link rel="stylesheet" type="text/css" href="/jquery.datetimepicker.min.css"/>

  <script type="text/javascript">
   jQuery('#datetimepicker').datetimepicker();

</script>
</head>
<body>
 

    
    <input id="datetimepicker" type="text" >
 
 
</body>
<script src="/jquery.js"></script>
<script src="/build/jquery.datetimepicker.full.min.js"></script>
</html>

So I found the original files hereenter link description here

and then changed my code to the following

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Home</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.3.7/jquery.datetimepicker.min.css"/>

  <script type="text/javascript">
   jQuery('#datetimepicker').datetimepicker();

</script>
</head>
<body>
 

    
    <input id="datetimepicker" type="text" >
 
 
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js" integrity="sha512-AIOTidJAcHBH2G/oZv9viEGXRqDNmfdPVPYOYKGy3fti0xIplnlgMHUGfuNRzC6FkzIo0iIxgFnr9RikFxK+sw==" 
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.js" integrity="sha512-9yoLVdmrMyzsX6TyGOawljEm8rPoM5oNmdUiQvhJuJPTk1qoycCK7HdRWZ10vRRlDlUVhCA/ytqCy78+UujHng==" 
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</html>

but now I have a new error. Any ideas on what I am missing?

enter image description here


Solution

  • Mind the order and dependencies of your scripts and use eventlisteners:

    To make it simple: Load CSS first (DateTimePicker), load JS second (jQuery, Datetimepicker), initialize an eventlistener using $().ready().

    <!doctype html>
    <html lang="en">
    <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Home</title>
      
      <!-- Load CSS first -->
      <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.3.7/jquery.datetimepicker.min.css"/>
      
      <!-- Load JS second -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js" integrity="sha512-AIOTidJAcHBH2G/oZv9viEGXRqDNmfdPVPYOYKGy3fti0xIplnlgMHUGfuNRzC6FkzIo0iIxgFnr9RikFxK+sw==" 
    crossorigin="anonymous" referrerpolicy="no-referrer"></script>
      
      <!-- Initialize JS, setup eventlistener(s) -->
      <script type="text/javascript">
       jQuery(document).ready($ => $('#datetimepicker').datetimepicker());
      </script>
      
    </head>
    
    <body>
        <input id="datetimepicker" type="text" >
    </body>
    
    </html>