I’m trying to make a functionning date picker but if I add a second one, I can’t add the date in the second form and I can’t write into the input text. Is there a way to avoid this conflict ? Where does the problem come from ?
$(function() {
$('#datetimepicker1').datetimepicker({
format: 'YYYY-MM-DDTHH:MM:SS',
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/css/bootstrap-datetimepicker.min.css">
<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
5
Answers
Problem is you are duplicating the ID. That is making a lot of confusion. Either change the ID and initialize your datepicker by grouping, like,
or, use a class, like
and
You are using the same id that’s conflict the input field change id
The html attribute id must be unique, just set the second time picker id to
#datetimepicker2
:Because you have HTML elements with the same
id
attribute.1. Make them unique.
2. You can modify your function by selecting the same class both of you elements have:
id
in html is unique. You cannot have multiple elements having the same id. Instead, for the above use case use classes.Refer https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id