0

I am trying to access datepicker() for each textbox dynamically added. here is my code :

  <div id="parent">
     Date: <input type="text" class="datepicker"><input type="button"value="Add"  id="btn"> 
  </div>





 $(function() {
$( ".datepicker" ).datepicker();
 });

  $(document).ready(function () {

    $("#btn").click(
        function () {
            AlertSave();
        }            
    );
});

  function AlertSave() {
  var textBox = document.createElement("input");
  textBox.setAttribute("class","datepicker");
document.getElementById("parent").appendChild(textBox);


   }

But my problem is , the first texbox shows datepicker. but the newly added textfield doen't shows datepicker. What's wrong with my code?

fiddle:https://jsfiddle.net/nguc6y4L/

Rehan
  • 33
  • 3

1 Answers1

1

You just need to initialize datepicker after adding new element. Just add $( ".datepicker" ).datepicker(); after adding new textbox. It will work.

Check: https://jsfiddle.net/koqppkuf/

Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27