0

I have view with two inputs where dates loading

Here is code in View of these inputs

<div class="device-selection">
    <label for="startDate">@Dictionary.General.StartDate: </label>
    <input id="startDate" class="round-input text-right" readonly />
</div>

<div class="device-selection">
    <label for="endDate">@Dictionary.General.EndDate: </label>
    <input id="endDate" class="round-input text-right" readonly />
</div>

In JS I have this code for showing calendar and populating inputs with dates:

  $(document).ready(function () {

//cultureTwoLetterName is a global variable from the view that contains the two letter iso language name
    $.datepicker.setDefaults($.datepicker.regional[cultureTwoLetterName]);

    $('#calendar').weekMonthDatepicker({
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        showWeek: true,
        firstDay: 1,
        weekHeader: '<span class="glyphicon glyphicon-arrow-down"></span>',
        minDate: -loggingRetention,
        maxDate: '+0D',
        weekSelection: true,
        weekSelected: SelectionCallback,
        monthSelection: true,
        monthSelected: SelectionCallback
    });

    $('#startDate').datepicker({
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        firstDay: 1,
        minDate: -loggingRetention,
        maxDate: '+0D'
    });


    $('#endDate').datepicker({
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        firstDay: 1,
        minDate: -loggingRetention,
        maxDate: '+0D'
    });


    $('#calendar').on('change', function () {
        $('#startDate').val(moment($(this).val()).format('DD/MM/YYYY'));
        $('#endDate').val(moment($(this).val()).format('DD/MM/YYYY'));
        var start = $('#calendar').weekMonthDatepicker('getStartDate');
        var stop = $('#calendar').weekMonthDatepicker('getEndDate');
        var ui;

        if (start != null && stop != null) {
            ui = { 'startDate': start, 'endDate': stop };

        }

        PopulateTableDetails(null, ui);
    });

    $('#startDate').on('change', function () {
        $(this).val(moment($(this).val()).format('DD/MM/YYYY'));

        if ($('#endDate').val() == '' || moment($('#endDate').val(), 'DD/MM/YYYY') < moment($(this).val(), 'DD/MM/YYYY')) { //endDate niet ingevuld of < startDate --> endDate = startDate
            $('#endDate').val($(this).val());
        }

        var start = moment($(this).val(), 'DD/MM/YYYY');
        var stop = moment($('#endDate').val(), 'DD/MM/YYYY');

        var ui = { 'startDate': start, 'endDate': stop };

        PopulateTableDetails(null, ui);
    });

    $('#endDate').on('change', function () {
        $(this).val(moment($(this).val()).format('DD/MM/YYYY'));

        if ($('#startDate').val() == '' || moment($('#startDate').val(), 'DD/MM/YYYY') > moment($(this).val(), 'DD/MM/YYYY')) { //startDate niet ingevuld of > endDate --> startDate = endDate
            $('#startDate').val($(this).val());
        }

        var start = moment($('#startDate').val(), 'DD/MM/YYYY');
        var stop = moment($(this).val(), 'DD/MM/YYYY');
        var ui = { 'startDate': start, 'endDate': stop };

       });
    }); 

I need to get values from startDate and endDate

So I try to write var startvalue = $('#startDate').val();

In this function $('#calendar').on('change', function () {

But I get empty alert with this code.

How I can get value of it correctly?

Thank's for help so much.

3
  • Please post this on jsbin so we can better assist you. Commented Nov 8, 2017 at 15:08
  • I'm not sure, but isn't readonly attribute preventing datepicker to set its value? Commented Nov 8, 2017 at 15:08
  • I think nope. Because it likes that it try to get value before it sets to input@Mathew Commented Nov 8, 2017 at 15:09

1 Answer 1

1

Your approach seems to be OK. Just try to remove the readonly attributes from inputs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.