Adding datepicker with jquery

I want to allow users to add new date fields to a form using jQuery, but the new date fields are just plain text fields without the popup datepicker.

This PHP code:

echo '
<a href="#" class="elgg-button-submit-element clone-datepicker" rel="nofollow">+</a>
    Works:  
        '.elgg_view('input/date', array('name' => 'date[]',)).'
    Doesn\'t work: 
 <div class="new_datefield"></div>

<script>
$(".clone-datepicker").live("click", function(e){
    e.preventDefault();
    
    // clone the node
    var html = $(".datefield").html();
    $(html).insertBefore(".new_datefield");
});</script>

<div id="line_store" style="visibility: hidden; display:inline-block;">
    <div class="datefield">  
        '.elgg_view('input/date', array('name' => 'date[]',)).'
    </div>
</div>';

Generates this HTML code.  The snapshot below was taken after cloning the datepicker field once:

<a href="#" class="elgg-button-submit-element clone-datepicker" rel="nofollow">+</a>
    Works:  
        <input type="text" value="" name="date[]" class="elgg-input-date popup_calendar hasDatepicker" id="dp1494426891315">
    Doesn't work:
        <input type="text" value="" name="date[]" class="elgg-input-date popup_calendar hasDatepicker" id="dp1494426891316">

 <div class="new_datefield"></div>

<script>
$(".clone-datepicker").live("click", function(e){
    e.preventDefault();
    
    // clone the node
    var html = $(".datefield").html();
    $(html).insertBefore(".new_datefield");
});</script>

<div id="line_store" style="visibility: hidden; display:inline-block;">
    <div class="datefield">  
        <input type="text" value="" name="date[]" class="elgg-input-date popup_calendar" />
    </div>

The original input field provides the calendar popup while the new input field, added with jQuery, does not.  What can I do to cause the new input field to also provide the calendar popup?

For what it's worth, each cloned input field has the same 'id' value.  I don't know whether that matters or how to affect it.

Thank you,

 - Scott

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking