Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
Calling datepicker options function from inside another function

I have the following function that is using a promise to return data from asynchronous ajax call:

$("#mySelect").on('change', function() {
      var mySelectValue = $('#mySelect').val();
      var promise = getAvailableDates(mySelectValue);

      promise.done( function(data) { // tested and returning data

        var array = data;

        $('#a_date').datepicker({ // this function not working
           dateFormat: "yy-mm-dd",
           beforeShowDay: function (date) {      
               var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
               return [array.indexOf(string) >= 0 ]
           }
        });

      });
   });

Why the $('#a_date').datepicker() function doesn't work inside another function?

It doesn't respond to my dateFormat and beforeShowDay settings based on data returned from my Ajax call.

How can I adjust Datepicker settings from inside a returned promise?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have to destroy the first datetimepicker object created before reinitialize it, by the following statement:

$("#datepicker").datepicker("destroy");

see follow:

$( function() {
    $( "#datepicker" ).datepicker({
      dateFormat: "yy-mm-dd"
    });
    
    $("#btn1").click(function(){
      $("#datepicker").datepicker("destroy");
      $("#datepicker").datepicker({
        dateFormat: "dd/mm/yy"
      });
      console.log("Format changed, try to select a new date");
    });
  } );
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date: <input type="text" id="datepicker"></p>
<input type="button" id="btn1" value="Change format">

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda