Search code examples
javascriptjquerytimepicker

trigger change event after picking from dropdown


I have a JQuery timepicker defined as

<div class="form-group">  
    <label class="col-xs-4 col-md-3 control-label" align="right">
        <div class="pull-right">{{ jc_settings_form.meeting_time.label() }}</div>
    </label>
    <div class='col-xs-8 col-md-9 dateContainer' style="max-width:200px">
        <div class='input-group input-append date'>
            <input type="select" class="form-control timepicker" name="meeting_time" id="meeting_time"/>
        </div>
    </div>
</div>

This is one option within a form and I would like to run automatic validation of the form whenever the user changes a field so I have some javascript like

$('.manage_jc_settings_form')
    .formValidation({
        framework: 'bootstrap',
        excluded: ':disabled',
        ignore: [],
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: { 
                meeting_time: {
                    validators: {
                        notEmpty: {
                            message: 'Please provide a meeting time'
                        }
                    }
                },...

for any reason if I pick a time from the timepicker dropdown it does not trigger the change even and therefore does not validate the form? If I click into the text field and change the time manually it does trigger the change even? Here is my timepicker initialisation

(function($) {
    $(function () {
        $('#meeting_time').timepicker({
            defaultTime: '{{ jc_settings_form.meeting_time.default }}',
            dropdown: true,
            scrollbar: true,
            startTime: '9:00 am',
            timeFormat: 'h:mm a', // equivalent to 'h:mm a'
            interval: 5 //Number of minutes the up/down arrow's will move the minutes value in the time picker
        });
    });
})(jQuery);

any idea how to force it to trigger the change event? I thought maybe I have to change the type="text" attribute of the input field?


Solution

  • You can run a function on the change event for jQuery Timepicker like this:

    (function($) {
        $(function () {
            $('#meeting_time').timepicker({
                defaultTime: '{{ jc_settings_form.meeting_time.default }}',
                dropdown: true,
                scrollbar: true,
                startTime: '9:00 am',
                timeFormat: 'h:mm a', // equivalent to 'h:mm a'
                interval: 5,
                change: function(time) {
                    alert('things changed');        
                }
            });
        });
    })(jQuery);
    

    It's listed on the docs here.