skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    The solution was to place the script BEFORE the form. Problem solved.


  2. You must use "onSelect" event. Look this fiddle.

    http://jsfiddle.net/0zdn8fh4/

    $( 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!');
            }
          });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search