2

I am trying to call the datepicker on dynamically created elements but doesn't work. I know that my format is not correct. Anyone knows how?

    $("body").on('click', '.startNew', function() {
        $(".startNew").datepicker({
           dateFormat : "yy-mm-dd 00:00:00",
           numberOfMonths: 2,
           onSelect: function(selected) {
             $(".endNew").datepicker("option","minDate", selected)
           }
        });
    });
    $("body").on('click', '.endNew', function() {
       $(".endNew").datepicker({ 
           dateFormat : "yy-mm-dd 00:00:00",
           numberOfMonths: 2,
           onSelect: function(selected) {
              $(".startNew").datepicker("option","maxDate", selected)
           }
       });
    });
1
  • 1
    we need to see where you are creating the element, include that source and the markup as well Commented Feb 18, 2015 at 2:39

2 Answers 2

2

Updated. Try to change (body -> document; click -> focus)

from

$(body).on('click',".startNew", function(){
    $(".startNew").datepicker({

$(body).on('click',".endNew", function(){
    $(".endNew").datepicker({ 

to

$(document).on('focus',".startNew", function(){
    $(this).datepicker({

$(document).on('focus',".endNew", function(){
    $(this).datepicker({ 

Working FIDDLE

HTML

Start: <input class='endNew'><br/><br/>
End: <input class='startNew'>

JS

$(document).on('focus',".startNew", function(){
  $(this).datepicker({
    dateFormat : "yy-mm-dd 00:00:00",
    numberOfMonths: 2,
    onSelect: function(selected) {
      $(".endNew").datepicker("option","minDate", selected)
    }
  });
});


$(document).on('focus',".endNew", function(){
  $(this).datepicker({ 
    dateFormat : "yy-mm-dd 00:00:00",
    numberOfMonths: 2,
    onSelect: function(selected) {
      $(".startNew").datepicker("option","maxDate", selected)
    }
   });
});
Sign up to request clarification or add additional context in comments.

1 Comment

this wouldn't work on the dynamically created elements.
0

The problem is the first click is initializing the plugin, but that won't show the datepicker since the datepicker click handler is added after the click event. So

$("body").on('click', '.startNew', function () {
    if (!$(this).hasClass('hasDatepicker')) {
        $(this).datepicker({
            dateFormat: "yy-mm-dd 00:00:00",
            numberOfMonths: 2,
            onSelect: function (selected) {
                $(".endNew").datepicker("option", "minDate", selected)
            }
        }).datepicker('show');
    }
});
$("body").on('click', '.endNew', function () {
    if (!$(this).hasClass('hasDatepicker')) {
        $(this).datepicker({
            dateFormat: "yy-mm-dd 00:00:00",
            numberOfMonths: 2,
            onSelect: function (selected) {
                $(".startNew").datepicker("option", "maxDate", selected)
            }
        }).datepicker('show');
    }
});

In this case, you are trying to initialize the plugin lazily, but a better solution will is to initialize the plugin after the target element .startNew and .endNew is created.

1 Comment

is there a way to call the datepicker through on click? or how can I re-initialize the plugin after .startNew and .endNew?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.