πDate picker
Add a date picker using an Open Text question



How to use it...
Variables
Variable
Value
Example
Examples:
Conclusion

Last updated
Add a date picker using an Open Text question




Last updated
function add_datepicker({question_code, min_date, max_date} = {}) {
input_date = document.querySelector("#p_" + question_code + "_1");
datepicker = `<input class="form-control" type="date" id="datepicker_`+ question_code + `" style="margin-top: 15px;">`;
input_date.style.display = "none";
input_date.outerHTML += datepicker;
datepicker = document.querySelector("#datepicker_" + question_code);
if(input_date.value != "") {
datepicker.value = input_date.value;
}
if(min_date !== undefined) {
datepicker.min = min_date;
}
if(max_date !== undefined) {
datepicker.max = max_date;
}
datepicker.addEventListener("change", (e) => {
input_date = document.querySelector("#p_" + question_code + "_1");
input_date.value = e.target.value;
})
document.querySelector("#p_next").addEventListener("click", (e) => {
if(min_date !== undefined && new Date(input_date.value) < new Date(min_date)) {
console.log(input_date, min_date);
e.preventDefault();
target_question = datepicker;
feedback = document.querySelector("#feedback_" + question_code);
if(feedback != null){
feedback.remove();
target_question.classList.add("is-invalid")
}
target_question.insertAdjacentHTML(
"beforebegin",
`<span class="d-block custom-error pb-1 text-center" id="feedback_` + question_code + `">
<span class="form-error-message text-danger">You cannot select a date earlier than `+min_date+`
</span></span>`
);
target_question.scrollIntoView({ behavior: "smooth", block: "start", inline: "nearest" });
} else if(max_date !== undefined && new Date(input_date.value) > new Date(max_date)) {
console.log(input_date, max_date);
e.preventDefault();
target_question = datepicker;
feedback = document.querySelector("#feedback_" + question_code);
if(feedback != null){
feedback.remove();
target_question.classList.add("is-invalid")
}
target_question.insertAdjacentHTML(
"beforebegin",
`<span class="d-block custom-error pb-1 text-center" id="feedback_` + question_code + `">
<span class="form-error-message text-danger">You cannot select a date later than `+max_date+`
</span></span>`
);
target_question.scrollIntoView({ behavior: "smooth", block: "start", inline: "nearest" });
} else if(input_date.value == "") {
e.preventDefault();
target_question = datepicker;
feedback = document.querySelector("#feedback_" + question_code);
if(feedback != null){
feedback.remove();
target_question.classList.add("is-invalid")
}
target_question.insertAdjacentHTML(
"beforebegin",
`<span class="d-block custom-error pb-1 text-center" id="feedback_` + question_code + `">
<span class="form-error-message text-danger">Please, choose a valid date
</span></span>`
);
target_question.scrollIntoView({ behavior: "smooth", block: "start", inline: "nearest" });
}
});
}
/* Change this part of the code with your custom parameters */
add_datepicker({
question_code : "Q1",
min_date : "yyyy-mm-dd",
max_date : "yyyy-mm-dd"
});add_datepicker({
question_code : "Q1",
min_date : "yyyy-mm-dd",
max_date : "yyyy-mm-dd"
});add_datepicker({
question_code : "Q1",
min_date : "yyyy-mm-dd",
max_date : "yyyy-mm-dd"
});add_datepicker({
question_code : "Q1",
min_date : "yyyy-mm-dd",
max_date : "yyyy-mm-dd"
});add_datepicker({
question_code : "Q1",
min_date : "yyyy-mm-dd",
max_date : "yyyy-mm-dd"
});min_date = new Date().toISOString().split('T')[0]
// or
max_date = new Date().toISOString().split('T')[0]add_datepicker({
question_code : "Q1"
});add_datepicker({
question_code : "Q1",
max_date : new Date().toISOString().split('T')[0],
});add_datepicker({
question_code : "Q1",
min_date : new Date().toISOString().split('T')[0]
});add_datepicker({
question_code : "Q1",
min_date : "2000-01-01",
max_date : "2022-12-31"
});