0

In Django Admin, the DateField with the built-in Today and Calendar anchors (AdminDateWidget)..how do I capture the event after the date is selected from the calendar?

I have tried every different event instead of click, and still does not work.

$("div.form-row.field-construction_end_date a[id^=calendarlink1]").on("click",
    function () {
    alert("works");
arc
  • 37
  • 6

1 Answers1

0

I had to go into the Django libraries and step through the code for calendar.js and datetimeshortcuts.js to see what was happening. On line 410 of datetimeshortcuts.js, I inserted this code to fire an event manually. Now it works:

if ("createEvent" in document) {
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("change", true, true);
    DateTimeShortcuts.calendarInputs[num].dispatchEvent(evt);
}
else
    DateTimeShortcuts.calendarInputs[num].fireEvent("onchange");

And that code, I got from here:

How can I trigger an onchange event manually?

So now, the last section of code in there around line 410 looks like this:

                .replace('\r', '\\r')
                .replace('\n', '\\n')
                .replace('\t', '\\t')
                .replace("'", "\\'");
            return function(y, m, d) {
                DateTimeShortcuts.calendarInputs[num].value = new Date(y, m - 1, d).strftime(format);
                DateTimeShortcuts.calendarInputs[num].focus();
                document.getElementById(DateTimeShortcuts.calendarDivName1 + num).style.display = 'none';
                if ("createEvent" in document) {
                    var evt = document.createEvent("HTMLEvents");
                    evt.initEvent("change", true, true);
                    DateTimeShortcuts.calendarInputs[num].dispatchEvent(evt);
                }
                else
                    DateTimeShortcuts.calendarInputs[num].fireEvent("onchange");
            };
        },
arc
  • 37
  • 6