Talent Runners
Talent Runners

Reputation: 419

Jquery UI datepicker is not working in dynamic fields

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>

Upvotes: 1

Views: 895

Answers (2)

Abhishek Pandey
Abhishek Pandey

Reputation: 13558

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>

Upvotes: 2

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48357

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>

Upvotes: 1

Related Questions