1

I'm adding dynamic input fields, but datepicker not working in dynamically generated fields.

$(document).ready(function() {
  $('#text').datepicker();
})

function addmore() {
  var html = '<div class="form-group"><input type="text" placeholder="text" id="text"><button type="button" onclick="removeInput(this)">&times;</button></div>';
  $('.input-fields').append(html);
}

function removeInput(obj) {
  $(obj).parent().remove();
}
.form-group {
  margin-bottom: 10px;
}
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<div>
  <div class="input-fields">
    <div class="form-group">
      <input type="text" placeholder="text" id="text">
    </div>
  </div>
  <button type="button" onclick="addmore()">Add more</button>
</div>

2 Answers 2

2

You need to generate new Id for every appended input, and then recall datepicker function.

var i = 0;
$(document).ready(function(){
  $('#text').datepicker();
})
function addmore(){
    i++
    var html = '<div class="form-group"><input type="text" placeholder="text-'+i+'" id="text-'+i+'"><button type="button" onclick="removeInput(this)">&times;</button></div>';
    $('.input-fields').append(html);
     $('#text-'+i).datepicker();    
}
function removeInput(obj){
    $(obj).parent().remove();
}
.form-group {
  margin-bottom: 10px;
}
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<div>
  <div class="input-fields">
    <div class="form-group">
      <input type="text" placeholder="text" id="text">
    </div>
  </div>
  <button type="button" onclick="addmore()">Add more</button>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for quick response.
1

The simpliest way is to use text class. You used text id, and that's a wrong way because when you add items, you will try to attach event for more elements with same id.

$(document).ready(function() {
  $('.text').datepicker();
})

function addmore() {
  var html = '<div class="form-group"><input type="text" placeholder="text" class="text"><button type="button" onclick="removeInput(this)">&times;</button></div>';
  $('.input-fields').append(html);
  $('.text').datepicker();
}

function removeInput(obj) {
  $(obj).parent().remove();
}
.form-group {
  margin-bottom: 10px;
}
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<div>
  <div class="input-fields">
    <div class="form-group">
      <input type="text" placeholder="text" class="text">
    </div>
  </div>
  <button type="button" onclick="addmore()">Add more</button>
</div>

2 Comments

You answers is shorter than other one, but using unique ID is much better I guess.
@TalentRunners, this should be the accepted answer, as it's a better approach considering the use of class instead of id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.