I have a custom datepicker like this
<input id='mydatepicker' data-mddatetimepicker='true' data-placement='left' data-trigger='focus' data-enabletimepicker='false' />
When I use it in html code work well. For example :
<div class="col-md-4" id="mydiv">
<input id='mydatepicker' data-mddatetimepicker='true' data-placement='left' data-trigger='focus' data-enabletimepicker='false' />
</div>
But I want add this datepicker using replaceWith
in document.ready
.
<div class="col-md-4" id="mydiv">
<div id="div2"></div>
</div>
<script>
$(document).ready(function () {
$("#div2").replaceWith("<input id='mydatepicker' data-mddatetimepicker='true' data-placement='left' data-trigger='focus' data-enabletimepicker='false' />");
});
</script>
But when page loaded just display an simple input without datepicker. How can I do this replacement correctly?
I use this library for datepicker
2
Answers
Try initialize the datepicker after replacing the content. Something like
Once you replace
#div2
with your input html, you need to initialize your datepicker with follwoing code as suggested in documentation [1].See the following example.