javascriptjquerycssjquery-mobiledatebox

How to make Datebox Jquery Mobile Input field clickable?


I have two date fields. Field 2 works fine. Clicking anywhere on the input type date for second fields opens the calendar. But the same is not working for first date field. When I click on the calendar icon for date field it opens the calendar but I want that calendar should pop up when I click anywhere on the date field for field 1 as well. Why does it works for the second field but not for the first field.

Also the default date is not getting set to todays date.

http://jsfiddle.net/KJ4e5/3/

HTML

<body>
    <div id="doc">
<table>
<tr>
                        <td>

                         <input name="date1" id="date1" type="date"  data-role="datebox" data-options='{"mode": "calbox","dateFieldOrder":["d","m","y"], "useNewStyle":true,"overrideDateFormat": "%d/%m/%Y", "afterToday":true}' />
                        </td>
                        <td>

                            <input name="date2" id="date2" type="date" data-role="datebox" data-options='{"mode": "calbox","dateFieldOrder":["d","m","y"], "useNewStyle":true,"overrideDateFormat": "%d/%m/%Y", "afterToday":true}'/>
                        </td>
                    </tr>
</table>
        </div></body>

//JS

$( document ).on( "pageinit", "#doc", function() { 
    var defaultPickerValue = new Date();
    var today = defaultPickerValue.getDate() + "/" + (defaultPickerValue.getMonth()+1) + "/" + defaultPickerValue.getFullYear();

     $('#date1').val(today);
     $('#date2').val(today);

$('#date1').on('change', function() {
        if ( $('#date2')) {
            $('#date2').val($('#date1').val());
              var temp = new Date();
              var start = $('#date1').datebox('getTheDate');
              var diff = parseInt((start - temp) / ( 1000 * 60 * 60 * 24 ));
              diffstrt = (diff * -1)-1; 
            $("#date2").datebox("option", {
                "minDays": diffstrt
            });
        }
    });

});

Solution

  • I can't explain why the 2 boxes act differently, but if you explicitly assign the useFocus option to the boxes, they will both work:

    <input name="date1" id="date1" type="date" data-role="datebox" 
        data-options='{"mode": "calbox","dateFieldOrder":["d","m","y"], 
                       "useNewStyle":true, "overrideDateFormat": "%d/%m/%Y",
                       "afterToday":true, "centerHoriz": true, "useFocus": true}' />
    

    Here is your updated FIDDLE

    In the fiddle I have also changed your script a little. With the datebox you can set the dates as follows:

    var defaultPickerValue = new Date();
    $('#date1').datebox('setTheDate', defaultPickerValue).trigger('datebox', {'method':'doset'});
    
    
    $('#date1').on('change', function () {
        if ($('#date2')) {
            var temp = new Date();
            var start = $('#date1').datebox('getTheDate');
            $('#date2').datebox('setTheDate', start).trigger('datebox', {'method':'doset'});
            var diff = parseInt((start - temp) / (1000 * 60 * 60 * 24));
            diffstrt = (diff * -1) - 1;
            $("#date2").datebox("option", {
                "minDays": diffstrt
            });
        }
    });