Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Datetimepicker Trigger "dp.change" on Class

I'm using this package for my datepicker. I'm initiating the plugin like this (CS):

$('.datepicker').datetimepicker({
    pickTime: false,
    language: 'en'
    })
.on('dp.change', (e) =>{
    console.log 'this'
})

The datepicker works fine, but the dp.change event is not triggered, anyone can explain why?

[DIRTY SOLUTION]

For now I'm using something like this, very ugly solution though:

$('.bootstrap-datetimepicker-widget').on("click","td.day", () => {
    date = $('.datepicker input[type=text]').val()
    $('.datepicker p').find('span').text(date)
})
like image 770
user2002495 Avatar asked Jul 04 '26 17:07

user2002495


2 Answers

The documentations https://eonasdan.github.io/bootstrap-datetimepicker/ Gives you an example on how to do this. (Just tested it and if you copy+paste the code, it will work fine.)

        $("#datetimepicker6").on("dp.change", function (e) {
//do your action here
        });

The ID of the jquery selector, you put the element you activated the datepicker for (i.e. initiated it like:

$('#datetimepicker6').datetimepicker();

) Let me know if it didn't work out for you :-)

like image 172
Adam Touhou Avatar answered Jul 07 '26 08:07

Adam Touhou


If you don't know the DOM ID of the element, you may listen to events on the document:

$(document).on('dp.change', function (e) {
  // here `e` is an event itself.
  // So, `e.target` should be a DOM element of the input field.
  if ($(e.target).data('object') === 'calendar1') {
      console.log('calendar1 just changed');
  } else {
      console.log('nope, we are waiting for calendar1 changes only');
  }
});

I hope this helps.

like image 25
Developer Avatar answered Jul 07 '26 08:07

Developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!