Django Bootstrap Datepicker Plus Events

Written by Scratch on Friday March 24, 2023

How to capture a change to a django-bootstrap-datepicker-plus field in javascript.

Problem

Trying to intercept the change event (dp.change) on the datepicker/datetimepicker field is tricky, nothing I found on the web worked.

Solution

Using django-bootstrap-datepicker-plus as a field widget injects a containing div and other elements. 

Debugging through the jquery code and generated html, I realised that the change event had to be registered on the div containing the input element.


// Given a Django model field called "starts_at", we use:

$("#id_starts_at").parent().on('dp.change', function (e) {
    var formatedValue = e.date.format(e.date._f);
    console.log(formatedValue);
});

References

Bootstrap Datepicker Plus

Bootstrap Datepicker

Category: software Tags: django python

Related content