Search code examples
eventsjquery-mobiledatebox

Date Changed Event for JQueryMobile datepicker


I'm using the Datebox for JQueryMobile, but I can't find any way to hook in to the date changing event (probably missing something obvious!) I'm using the calendar inline so there is no text field available if that makes a difference.

Is there a way to trigger an event when I select a date in the calendar? Thanks, Becky


Solution

  • Solution

    This examples is made on a basis of a DateBox2, tell me if you are using older version.

    Working example: http://jsfiddle.net/Gajotres/ktbcP/15/

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <link type="text/css" href="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.min.css" rel="stylesheet" />       
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>        
        <script type="text/javascript" src="http://dev.jtsage.com/jquery.mousewheel.min.js"></script>
        <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/<jqm-date></jqm-date>box.core.min.js"></script>
        <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.mode.calbox.min.js"></script>
        <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.mode.datebox.min.js"></script>
        <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.mode.flipbox.min.js"></script>
        <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.mode.durationbox.min.js"></script>
        <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/latest/jqm-datebox.mode.slidebox.min.js"></script>
        <script type="text/javascript" src="http://dev.jtsage.com/cdn/datebox/i18n/jquery.mobile.datebox.i18n.en_US.utf8.js"></script>      
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="a" data-role="header">
                <h3>
                    First Page
                </h3>
                <a href="#second" class="ui-btn-right">Next</a>
            </div>
    
            <div data-role="content">
                <label for="mydate">Some Date</label>
                <input name="mydate" id="mydate" type="date" data-role="datebox" data-options='{"mode": "datebox", "useNewStyle":true}'/>
            </div>
    
            <div data-theme="a" data-role="footer" data-position="fixed">
    
            </div>
        </div>   
    </body>
    </html>   
    

    Javascript:

    $(document).on('pageinit', '#index', function(){ 
        $('#mydate').bind('datebox', function(e, p) {
            if ( p.method === 'set' ) {
                e.stopImmediatePropagation()
                alert('Changed');
            }
        });
    });
    

    Explanation

    DateBox2 has several callback functions and set is one of them. What you need to do is bind a datebox event to a input box, like this:

    $('#mydate').bind('datebox', function(e, p) {
        if ( p.method === 'set' ) {
            e.stopImmediatePropagation()
            alert('Changed');
        }
    });
    

    And just check for an appropriate method. Their official documentation holds a full method list.

    Unfortunately event that detects manual date change don't exist, if this is what you need.