1

Consider the following code snippet :

    var from,to;
    to = $(".range-to-dt").persianDatepicker({
    inline: true,
    minDate: new persianDate(cleanDate(serverDateTime)),
    altField: '.range-to-dt-alt',
    altFormat: 'YYYY/MM/DD',
    initialValue: false,
    onSelect: function (unix) {
        to.touched = true;
        if (from && from.options && from.options.maxDate != unix) {
            var cachedValue = from.getState().selected.unixDate;
            from.options = { maxDate: unix };
            if (from.touched) {
                from.setDate(cachedValue);
            }
        }
    }
});
from = $(".range-from-dt").persianDatepicker({
    inline: true,
    observer: true,
    minDate: new persianDate(cleanDate(serverDateTime)),
    altField: '.range-from-dt-alt',
    altFormat: 'YYYY/MM/DD',
    initialValue: false,
    onSelect: function (unix) {
        from.touched = true;
        if (to && to.options && to.options.minDate != unix) {
            var cachedValue = to.getState().selected.unixDate;
            to.options = { minDate: unix };
            if (to.touched) {
                to.setDate(cachedValue);
            }
        }
    }
});

How can I use the js function on the same page more than once (in several different forms) to perform correctly?

<form id="form1" ...>
   <input asp-for="DateTimeRange.StartDate"  ltr-input range-from-dt-alt">
   <input asp-for="DateTimeRange.EndDate"  ltr-input range-to-dt-alt">
</form>
<form id="form2" ...>
    <input asp-for="DateTimeRange.StartDate"   ltr-input range-from-dt-alt">
   <input asp-for="DateTimeRange.EndDate"  ltr-input range-to-dt-alt">
</form>

Actually how to create Multiple instances on the same page of js function?

2
  • Please note that an id should always be unique. id="min-date-input" Commented Jun 23, 2020 at 13:07
  • @CarstenLøvboAndersen : Yes, that's right. I deleted it ... how about now? Commented Jun 23, 2020 at 13:57

1 Answer 1

1

You have to loop through each form containing the 2 inputs.

You can add a class to your form instead of ID for example, like

<form class="startEndForm">
   // Your 2 inputs here
</form>

And then do something like that :

$('.startEndForm').each(function () {
 $(this).find(".range-to-dt").persianDatepicker({
    inline: true,
    minDate: new persianDate(cleanDate(serverDateTime)),
    altField: '.range-to-dt-alt',
    altFormat: 'YYYY/MM/DD',
    initialValue: false,
    onSelect: function (unix) {
        var from = $(this).parent().find('.range-from-dt');
        var to = $(this);
        to.touched = true;
        if (from && from.options && from.options.maxDate != unix) {
            var cachedValue = from.getState().selected.unixDate;
            from.options = { maxDate: unix };
            if (from.touched) {
                from.setDate(cachedValue);
            }
        }
    }
});
$(this).find(".range-from-dt").persianDatepicker({
    inline: true,
    observer: true,
    minDate: new persianDate(cleanDate(serverDateTime)),
    altField: '.range-from-dt-alt',
    altFormat: 'YYYY/MM/DD',
    initialValue: false,
    onSelect: function (unix) {
        var from = $(this);
        var to = $(this).parent().find('.range-to-dt');
        from.touched = true;
        if (to && to.options && to.options.minDate != unix) {
            var cachedValue = to.getState().selected.unixDate;
            to.options = { minDate: unix };
            if (to.touched) {
                to.setDate(cachedValue);
            }
        }
    }
});
});
Sign up to request clarification or add additional context in comments.

6 Comments

Uncaught TypeError: can't access property "touched", from is undefined
@ ThéoBenoit : Uncaught TypeError: $(...).forEach is not a function But if I use each one, it's not a problem!
Ah yes my bad, it's .each() in jQuery ! I'll edit it ;)
@ ThéoBenoit : Thank you very much for your guidance. But there is still a problem. There are range-from-dt-alt in both forms and it causes information to enter both forms at the same time.
@farshid I edited the answer, pretty sure it comes from the "onSelect" not assigned from and to with the good input. If it's not working, could you please provide a codepen or something so I can better help
|

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.