0

Dynamically created date picker not working from created second row:

$(document).ready(function () {
    $(".add_button").click(function () {
        var currentRow = $(this).parents("tr");
        var nr = currentRow.clone(true, true);
        nr.find(".name").val('');
        currentRow.after(nr);
    });
    $(".del_button").click(function () {
        var currentRow = $(this).parents("tr");
        if ($(".test tr").length != 2)
        {
            $(this).closest('tr').remove();
        }
        else
        {
            alert("You cannot delete first row");
        }
    });
});


<script>
    $(function () {
        $("#datepicker").datepicker({autoclose: true});
    });
</script>
tejashsoni111
  • 1,405
  • 1
  • 17
  • 32

1 Answers1

0

The datepicker will not be automatically bind with the dynamically created element. You have to bind it after you create the element with unique id and bind date picker.

Try this:

$(document).ready(function () {
    var current_id = 1;
    $(".add_button").click(function () {
        var currentRow = $(this).parents("tr");
        var nr = currentRow.clone(true, true);
        nr.find(".name").val('');
        nr.find(".name").attr('id','name_'+current_id);
        currentRow.after(nr);
        $("#name_"+current_id).datepicker({autoclose: true});

    });
});
Govind Samrow
  • 8,677
  • 13
  • 46
  • 76