I am using Gravity Forms with the date picker, and I need to show an alert if someone clicks a weekend. Here is my code:
$( document ).ready(function() {
$("#input_2_3").datepicker({ // ID of the date picker field
onClick: function (selectedDate) {
var date = $(this).datepicker('getDate');
var day = date.getDay();
if (day === 6 || day === 7) {
alert('you clicked a weekend!');
}
}
});
});
The problem is that absolutely nothing happens, and no complaints from the console either.
What's going on? If it helps to know, my date format is dd-mm-yy
CodePudding user response:
You must use "onSelect" event. Look this fiddle.
$( document ).ready(function() {
$("#datepicker").datepicker({ // ID of the date picker field
onSelect: function () {
var date = $(this).datepicker('getDate');
var day = date.getDay();
if (day === 0 || day === 6) {
alert('you clicked a weekend!');
}
}
});
});
Edit, try adding one onchange, it should works:
$( document ).ready(function() {
$('#datepicker').datepicker();
$('#datepicker').on("input change", function (e) {
var date = $(this).datepicker('getDate');
var day = date.getDay();
if (day === 0 || day === 6) {
alert('you clicked a weekend!');
}
});
});
CodePudding user response:
The solution was to place the script BEFORE the form. Problem solved.