0
\$\begingroup\$

I'm using the Jquery date time picker link here

In the four methods below I am initializing the DT pickers but they are all pretty similar except for the ID in the onChangeDateTime.

Is there a way I can reduce the duplicate code?

$('.jqDatetimeDateOne').datetimepicker({
    defaultTime: '07:00',
    lang: 'en',
    className: 'setTimeClicker',
    step: 30,
    onChangeDateTime: function (current_time, $input) {
        // alert(ct.dateFormat('d/m/Y'))
        $('#emailGenDateOne').removeClass('empty').val(current_time.dateFormat('Y-m-d H:i'));
    },
    inline: true
});

$('.jqDatetimeDateTwo').datetimepicker({
    defaultTime: '07:00',
    lang: 'en',
    className: 'setTimeClicker',
    step: 30,
    onChangeDateTime: function (current_time, $input) {
        // alert(ct.dateFormat('d/m/Y'))
        $('#emailGenDateTwo').removeClass('empty').val(current_time.dateFormat('Y-m-d H:i'));
    },
    inline: true
});

$('.jqDatetimeSearchDateOne').datetimepicker({
    defaultTime: '07:00',
    lang: 'en',
    className: 'setTimeClicker',
    step: 30,
    onChangeDateTime: function (current_time, $input) {
        // alert(ct.dateFormat('d/m/Y'))
        $('#searchGenDateOne').removeClass('empty').val(current_time.dateFormat('Y-m-d H:i'));
    },
    inline: true
});

$('.jqDatetimeSearchDateTwo').datetimepicker({
    defaultTime: '07:00',
    lang: 'en',
    className: 'setTimeClicker',
    step: 30,
    onChangeDateTime: function (current_time, $input) {
        // alert(ct.dateFormat('d/m/Y'))
        $('#searchGenDateTwo').removeClass('empty').val(current_time.dateFormat('Y-m-d H:i'));
    },
    inline: true
});
\$\endgroup\$
3
\$\begingroup\$

So:

function configureDateTimePicker(selector1,selector2)
{
    $(selector1).datetimepicker(
    {
        defaultTime: '07:00',
        lang: 'en',
        className: 'setTimeClicker',
        step: 30,
        onChangeDateTime: function (current_time, $input)
        {
            // alert(ct.dateFormat('d/m/Y'))
            $(selector2).removeClass('empty').val(current_time.dateFormat('Y-m-d H:i'));
        },
        inline: true
    });
}

configureDateTimePicker('.jqDatetimeDateOne','#emailGenDateOne');
configureDateTimePicker('.jqDatetimeDateTwo','#emailGenDateTwo');
configureDateTimePicker('.jqDatetimeSearchDateOne','#searchGenDateOne');
configureDateTimePicker('.jqDatetimeSearchDateTwo','#searchGenDateTwo');

There could be further optimisations if you changed your classes/ids so that one could be derived from the other or from a common string (e.g. if they were all .jqDatetimeSomething and #genSomething). Or if there's a way to find the one of the targets through their relationship in the DOM (e.g. one is a descendant of the other matching a given selector).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.