user2761030
user2761030

Reputation: 1485

jQuery - Cloning a datepicker field

I'm trying to clone a table row and get a datepicker box working on all rows. At the moment, the first one works, but not later boxes.

Can anyone offer any tips? Any help much appreciated!

Here's the code:

HTML

<link rel="stylesheet" type="text/css" href="http://brenda.upreach.org.uk/plugins/jquery.datepick.package-4.1.0/redmond.datepick.css"> 

    <script type="text/javascript" src="http://brenda.upreach.org.uk/plugins/jquery.datepick.package-4.1.0/jquery.datepick.js">  </script>

    <div class="clone_Associate">
<input type="text" name="DATE_SET[]" class="datepick" value="04/12/2013">

    </div>

    <div class="placer_Associate"></div>

        <a href="#" class="clone_trigger_Associate">Clone a new datebox!</a>

jQuery

$(function() {
    $('.datepick').datepick({ 
            dateFormat: 'dd/mm/yyyy', showTrigger: '#calImg'});
});



$(document).ready(function(){
          $(".clone_trigger_Associate").click(function () {
              var total = $('[name^="UPDATE_METHOD"]').length;
              var index = Math.round(total / 2);
              $('.clone_Associate').last().clone().insertBefore(".placer_Associate");
              $('input.cl:last').val('');
              $('.clone_Associate').last().find("input[type='checkbox']").prop("name","UPDATE_METHOD["+index+"][]")

              // Date pick element
              $('.datepick').datepick({ 
                    dateFormat: 'dd/mm/yyyy', showTrigger: '#calImg'}
              );
              event.preventDefault();  
            });
        });

A jsfiddle is here: http://jsfiddle.net/dalepotter/aSG6e/

Upvotes: 1

Views: 739

Answers (2)

Aamir Afridi
Aamir Afridi

Reputation: 6411

Demo: http://jsfiddle.net/aSG6e/15/

$(function() {

 var options = {dateFormat: 'dd/mm/yy'}

 $('.datepick').datepicker(options);

 $(".clone_trigger_Associate").click(function (e) {
    e.preventDefault();
    var $newInput = $('.datepick:last').clone(true).removeAttr('id');
    $(this).before($newInput);
    $newInput.datepicker('destroy').datepicker(options);
 });
});

Upvotes: 1

BerAenT
BerAenT

Reputation: 1

Change this code;

$('.clone_Associate').last().clone().insertBefore(".placer_Associate");

with this;

var newDate = '<div class="clone_Associate"><input type="text" name="DATE_SET[]" class="datepick" value="04/12/2013"></div>';
$('.clone_Associate:last').after(newDate);

Upvotes: 0

Related Questions