I'm trying to use jquery's datepicker for dynamic form fields. This is my fiddle that show the issue.
below is my code
<div class="box-body" id="collapse1">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>
<red>*</red>
Full Name (As on NRIC/FIN)
</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label>
<red>*</red>
Gender
</label>
<div class="radio row">
<div class="col-sm-6">
<label class="radio-inline">
<input type="radio" id="male"> Male
</label>
</div>
<div class="col-sm-6">
<label class="radio-inline">
<input type="radio" id="female"> Female
</label>
</div>
</div>
</div>
</div>
<div class="col-md-3">
<form-group>
<label>
<red>*</red>
Date Of Birth (DD-MM-YYYY)
</label>
<input type="text" name="date" class="datepicker">
</form-group>
</div>
<div class="col-md-3">
<form-group>
<label>
<red>*</red>
NRIC / FIN
</label>
<input type="text" class="form-control">
</form-group>
</div>
</div>
</div>
<div id="parent" class="col-md-3">
<div id="child" class="col-md-3">
</div>
</div>
<br />
<div>
<button type="button" onclick="addInputLine()">
press
</button>
</div>
Steps to reproduce:
- click "press" button
- A new div with a second date input appears
Expected result: able to pick date for x amount of date input field created
Actual result: date pick on the second input field update the first field
What I have tried: I apply solution from this page, it does not work for me
I want to do this for any amount of inputs dynamically created by the user. Thanks for the help!
addInputLine()$(parent).find(".datepicker" ).datepicker({ dateFormat: "yyyy-mm-dd" });, but the datepicker isn't working in your fiddle - but strangely does work if you remove the first init: jsfiddle.net/xpmgLc83 I think it's because of the way you copy the html - it's copying thehasDatepickerclass so doesn't re-init as it thinks it's already a datepicker.