Re-initialize or destroy Bootstrap datepicker dynamically
Asked Answered
H

5

22

Is there a way to destroy the Bootstrap datepicker dynamically updating its options like format, beforeShowDay, etc.? I know that the jQuery UI datepicker has a destroy method but Bootstrap's has not. It only has the .('remove') method but its not working.

My goal is to modify the options of the datepicker when changing an input, for example:

$('#employee').change( function() {
   $('#date').datepicker('destroy'); //not working 'cause bootstrap hasnt a destroy method
   $('#date').hide();
});

Then I call the initialize function when the input changes:

function updateCalendar(){
     $('#date').datepicker({
          format:"dd/mm/yyyy",
          beforeShowDay: updateC  //function that modifies the available days to show
     });    
 }
Harned answered 10/5, 2014 at 14:40 Comment(0)
N
45
$('.datepicker').datepicker('remove');

Make sure you have your date picker object in DOM before removing it. Without removing you can hide the calendar and change the format of date and update it .

$('.datepicker').datepicker('update');
Nabokov answered 10/5, 2014 at 15:0 Comment(3)
it was destroying the datepicker before initializing for the first time. After checking that ir worked :) Thank you!Harned
remove has been deprecated, use destroyAltarpiece
If you are using the latest version of the jquery datepicker Instead of $('.datepicker').datepicker('remove'); use $('.datepicker').datepicker('destroy');Nabokov
D
2

You can Use:

$('#element_id').datepicker('destroy');

I called this just outside my ajax call. My ajax call was creating the datepicker dynamically. After the call was made I destroyed the object.

Like below:

$.ajax({ url: 'my_ business_functions.php',
     data: {func_call: 'my_method_name', my_parameter: my_parameters},
     type: 'post',
     success: function(output) {
         //all my stuffs here
        $('#element_id').val(datepicker_value);
        }
    });
    $('#element_id').datepicker('destroy');

This served my purpose.

Dawndawna answered 20/9, 2017 at 11:54 Comment(0)
S
0

I actually found it more to just remove the element's ID, clone it, and add the ID back in.

Swinford answered 21/5, 2015 at 7:1 Comment(1)
Yes, it would have been more helpful if you have shown us in sample codes what exactly you did that worked.Shielashield
E
0

Propably you are using datepicker with input btn group, I mean you with a date icon button. So, you should not choose just input element when you want to destroy it and reinit. You should select div has "date-picker", then remove and init again.

Excisable answered 3/9, 2016 at 14:27 Comment(0)
V
-2
forceParse: 1

In your options. It will parse the input date field value every time.

Vervet answered 14/8, 2017 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.