javascripthtmljqueryjquery-uijquery-ui-datepicker

how to get work schedule


I have a work schedule that has a two-week length. I want to make a simple html page with a datepicker and three textboxes:

Textbox3 has 14 different cases. I got the following code which doesn't work. Can someone please look into it and tell me what I'm doing wrong?

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Format date</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="/scripts/jquery.ui.datepicker-it.js"></script>

<script>
$(function() {
  var weekStart = new Date(2017, 1, 27);
  var roster = ['work', 'off', 'off', 'work', 'work', 'work', 'work',
  	'off', 'work', 'work', 'work', 'off', 'off', 'work'];
  $('#date').datepicker({minDate: weekStart, onSelect: function(dateStr) {
    var date = $(this).datepicker('getDate');
    var days = Math.floor((date.getTime() - weekStart.getTime()) / (24 * 60 * 60 * 1000));
    var week = Math.floor(days / 7) % 2;
    $('#week').text(week + 1);
    $('#day').text($.datepicker.formatDate('DD', date));
    $('#work').text(roster[week * 7 + date.getDay() - 1]);
  }});
});
</script>
</head>
<body>
 
<p>Date: <input type="text" id="date"></p>
<p>Day: <input type="text" id="day"></p>
<p>week: <input type="text" id="week"></p>
<p>work: <input type="text" id="work"></p>

</body>
</html>


Solution

  • You need to use val() to fill input boxes.

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>jQuery UI Datepicker - Format date</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script type="text/javascript" src="/scripts/jquery.ui.datepicker-it.js"></script>
    
    <script>
    $(function() {
      var weekStart = new Date(2017, 1, 27);
      var roster = ['work', 'off', 'off', 'work', 'work', 'work', 'work',
      	'off', 'work', 'work', 'work', 'off', 'off', 'work'];
      $('#date').datepicker({minDate: weekStart, onSelect: function(dateStr) {
        var date = $(this).datepicker('getDate');
        var days = Math.floor((date.getTime() - weekStart.getTime()) / (24 * 60 * 60 * 1000));
        var week = Math.floor(days / 7) % 2;
        $('#week').val(week + 1);
        $('#day').val($.datepicker.formatDate('DD', date));
        $('#work').val(roster[week * 7 + date.getDay() - 1]);
      }});
    });
    </script>
    </head>
    <body>
     
    <p>Date: <input type="text" id="date"></p>
    <p>Day: <input type="text" id="day"></p>
    <p>week: <input type="text" id="week"></p>
    <p>work: <input type="text" id="work"></p>
    
    </body>
    </html>