9

I have a page which contains a form and few fields. My issues are as follow:

  1. I am trying to force jQuery UI datepicker to display below the input field.
  2. When I click in the input field, I want the field to scroll to the top of the page also. I think I have this working.

Here is my jQuery:

JQUERY:

$( document ).ready(function() {
    // Set Datepicker
    $(".datepicker").datepicker();

    $("input").focus(function () {
       $('html, body').animate({ scrollTop: $(this).offset().top - 25 }, 10);
   });

});

Link to a fiddle: https://jsfiddle.net/MauriceT/0qfycgm1/10/

Here is what I want to avoid:

Datepicker displays above the input field

Any suggestions would be greatly appreciated. Thanks!

Maurice Twomey
  • 157
  • 1
  • 3
  • 11
  • what do you meant by below the input ? Do you want the input field to overlay over the date picker ? – Deep Jul 15 '16 at 15:13
  • So I need the datepicker to display below the field no matter what. At the moment, If you click on the datepicker and there is space above the field, the datepicker will appear above the field. Click the link in the image of my question for a visual. Thanks! – Maurice Twomey Jul 15 '16 at 15:19

4 Answers4

17

you can achieve your goal by setting the css of the date picker pop.

Use the below code to set the CSS, the use of setTimeout is to avoid the overriding of the CSS.

Here i am finding out the top and left of the date time picker text box and using these value to set the position of the date time picker popup

On the information about beforeShow event check here.

https://api.jqueryui.com/datepicker/#option-beforeShow

    $(".datepicker").datepicker({
    beforeShow: function (input, inst) {
        setTimeout(function () {
            inst.dpDiv.css({
                top: $(".datepicker").offset().top + 35,
                left: $(".datepicker").offset().left
            });
        }, 0);
    }
}); 

Here is the fiddle : https://jsfiddle.net/0qfycgm1/14/

Deep
  • 9,594
  • 2
  • 21
  • 33
1

A simple fix is by adjusting the frontend using CSS for the CALENDER Box.

Add the following to your CSS file.

    .ui-datepicker{
        margin-top: 300px;
    }

I tried it in your Fiddle link, was working perfectly fine.

Ankush Raghuvanshi
  • 1,392
  • 11
  • 17
  • Thanks for your response, Ankush! However, I'm not sure this is a robust solution because If I scroll down the page and select the datepicker, the datepicker will appear 300px below the field. I need something more dynamic. – Maurice Twomey Jul 15 '16 at 15:21
  • From what I comprehend, the CALENDER box is appearing above the date-picker input field, but you want it to be displayed below it.? Is it so, or do you want something else? – Ankush Raghuvanshi Jul 15 '16 at 16:00
  • Oh i get the SCROLL problem you mentioned. And it persists in case of top-margin: 0px also and hence Jayanti Lal's solution ain't working as well. – Ankush Raghuvanshi Jul 15 '16 at 16:13
  • Check this out http://stackoverflow.com/questions/15131465/how-to-change-jquery-ui-datepicker-position The same as specified by @Deep – Ankush Raghuvanshi Jul 15 '16 at 16:23
  • Thank you, Ankush! I have marked @Deep's answer as the resolved answer. – Maurice Twomey Jul 18 '16 at 14:11
1

add

       ui-datepicker{
          margin-top: 0px;
          }

I have added this code to your fiddle and have tested it. hope this will help. have updated your fiddle

Jayanti Lal
  • 1,175
  • 6
  • 18
0

Following line of code from jquery-ui-(version) sets your object top position to be zero so it is displayed wrong.Comment the line of code you will get what you want. I spend lots of days found no better solution than this.

offset = $.datepicker._checkOffset( inst, offset, isFixed );

Pashupati Khanal
  • 733
  • 3
  • 11