简体   繁体   中英

jquery datepicker click issues when datepicker created dynamically

I created datepicker using jquery on document ready method my code as below

$(document).ready(function(){
  $("#setdate").html('<input type="text" class="datepicker">');
 });

So my text box created at run time and I was added class name as datepicker when I click on that text box it not shows datepicker dialog because of it creates on page load times so I was added following code

 $(this).on("click", ".datepicker", function(){
     $(".datepicker").datepicker({
     changeMonth: true,
     changeYear: true 
   });
 });

When I clicked on datepicker textbox at first time it shows nothing but next click it opens datepicker dialog can any one knows why this happens, I want open datepicker on first click how it possible?

Your logic is slightly out of sync. You are only turning the text box into a datepicker when it is clicked on. This means before the first click it is just a regular text box. What you want to do is turn the text box into a datepicker on pageload.

This should work:

$(document).ready(function(){

  //create the text box
  $("#setdate").html('<input type="text" class="datepicker">');

  //initialise the datepicker code on the text box
  $(".datepicker").datepicker({
     changeMonth: true,
     changeYear: true 
   });

 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM