I have a form where multiple fields can be added with datepicker. However, when adding a new field to the form, the Jquery datepicker is not loaded.
This is the JS responsible for cloning and adding fields to the form:
$(document).ready(function () {
hideBtnDel();
function hideBtnDel() {
if ($("#digitalCertificates .digitalCertificate").length <= 1) {
$("#digitalCertificates .digitalCertificate .removeDigitalCertificate").addClass('disabled');
$("#digitalCertificates .digitalCertificate .removeDigitalCertificate").prop('disabled', true);
} else {
$("#digitalCertificates .digitalCertificate .removeDigitalCertificate").removeClass('disabled');
$("#digitalCertificates .digitalCertificate .removeDigitalCertificate").prop('disabled', false);
}
removeDigitalCertificate();
}
function removeDigitalCertificate() {
$(".removeDigitalCertificate").unbind("click");
$(".removeDigitalCertificate").bind("click", function () {
$(this).parents('.digitalCertificate').remove();
hideBtnDel();
});
}
$("#addDigitalCertificate").click(function () {
event.preventDefault();
newDigitalCertificate = $("#digitalCertificates .digitalCertificate:first").clone();
newDigitalCertificate.find("input[type='text']").val("");
newDigitalCertificate.find("input[type=hidden]").val("");
newDigitalCertificate.find("input[type='checkbox']").prop('checked', false);
newDigitalCertificate.find("select").val("");
newDigitalCertificate.insertAfter("#digitalCertificates .digitalCertificate:last");
hideBtnDel();
});
Doing a search, I found this, which in theory should solve the problem, but it did not work, it even goes into the function, but the datepicker is not applied (I added it shortly after the '$ (document) .ready (function () {...} 'of the above block):
$(document).on('focus', '.datepicker', function () {
console.log('teste');
$(this).datepicker({
dateFormat: 'dd/mm/yy',
dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'],
dayNamesMin: ['D', 'S', 'T', 'Q', 'Q', 'S', 'S', 'D'],
dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb', 'Dom'],
monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
monthNamesShort: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
nextText: 'Próximo',
prevText: 'Anterior',
});
});
Is there any other way to dynamically load this datepicker?