monim67 / django-bootstrap-datepicker-plus

Bootstrap3/Bootstrap4/Bootstrap5 DatePickerInput, TimePickerInput, DateTimePickerInput, MonthPickerInput, YearPickerInput with date-range-picker functionality for django >= 2.0
https://pypi.python.org/pypi/django-bootstrap-datepicker-plus
MIT License
223 stars 61 forks source link

Can't get an alert whenever the date on a datepicker changes #21

Closed joaoestudante closed 5 years ago

joaoestudante commented 5 years ago

Hello. I am trying to do an action whenever a date is picked on the datepicker.

Currently, my date picker portion of the html file looks like this (generated automatically by using the widget on a DateField)

<div class="input-group date">
    <input type="text" name="primeira_emissao_dia" required class="form-control form-control" id="id_primeira_emissao_dia" dp_config="{&quot;linked_to&quot;: null, &quot;options&quot;: {&quot;showTodayButton&quot;: true, &quot;showClear&quot;: true, &quot;showClose&quot;: true, &quot;format&quot;: &quot;YYYY-MM-DD&quot;}, &quot;picker_type&quot;: &quot;DATE&quot;, &quot;id&quot;: &quot;dp_1&quot;}"/>
    <div class="input-group-addon input-group-append" data-target="#datetimepicker1" data-toggle="datetimepickerv">
        <div class="input-group-text"><i class="glyphicon glyphicon-calendar"></i></div>
    </div>
</div>

For now, I would just like to be able to print in the console (or get an alert) whenever a date is chosen.

I attempted to add this after the body:

<script>
$("div.input-group.date").on('dp.change', function(e) {
      alert("Hello there.");
  });
</script>

And changing the date had no effect. I also tried to do it like this:

<script>
  $(document).ready(function () {
          (...)
          $("div.input-group.date").on('dp.change', function(e) {
            alert("Hello there.");
          });
      });
</script>

Again, with no effects.

The only thing that I noticed is that an event tag appears (on Inspect element, with Firefox on Linux) on the div, with a dp element on it, referencing my website:

example_event_change

What am I doing wrong? If there is more information needed, I'll be more than glad to offer it.

monim67 commented 5 years ago

I don't see anything wrong, the same code works fine for me.

Go to the demo page and paste the JavaScript code in browser console, it works.

joaoestudante commented 5 years ago

I can confirm that it works on the demo page. On the console on my website, pasting the function successfully returns the objects with the specified class (same as on the demo page).

However, nothing really happens when changing the dates. I believe this must be some sort of conflict with another part of my website (like a navbar).

Thanks for providing the "trick" of pasting the function on the console, at least I know I'm not insane anymore :D and as this is most likely not a problem with the (wonderful btw) module, I'll close the issue. If it appears to be some sort of unintended conflict, then I can reopen it.