• WORK
  • PEOPLE
  • ABOUT
  • BLOG
  • Blog
  • News
  • Events
  • Presentations
  • JQuery UI Datepicker IE focus fix

    By Jeff Sheets on Jun. 18 2012

    The jquery ui datepicker control is quite slick and easy to use, and as you see here it can be customized through various events.

    At my current client we have multiple controls on the page that listen for onblur and onchange events to notify of changes to the page (most notably an html5 placeholder enabler). But the datepicker does not send the blur event when a selection happens. Also, our users wanted focus to return the text input field after the date selection. So we need to setup some event handlers to call the blur and focus events upon date selection.

    First let’s start with a simple datepicker that is applied to any input field having css class dateInput, with a few extra options:

    $("input.dateInput").datepicker({
      changeMonth: true,
      changeYear: true,
      showAnim: "fadeIn",
      yearRange: 'c-30:c+30',
      showButtonPanel: true
    });
    

    Adding a blur and change event on datepicker selection is rather easy:

    $("input.dateInput").datepicker({
      changeMonth: true,
      changeYear: true,
      showAnim: "fadeIn",
      yearRange: 'c-30:c+30',
      showButtonPanel: true,
    
      /* blur needed to correctly handle placeholder text */
      onSelect: function(dateText, inst) {
        $(this).blur().change();
      }
    });
    

    But adding focus is a little more difficult because of a difference in browser behavior. Simply adding a .focus() to onSelect and onClose will suffice for Chrome and Firefox but IE will reopen the datepicker once it receives the focus. In order to handle IE we can simply implement the beforeShow event handler, returning false when we reach a case where IE should not reopen the datepicker window. I’ve added a fixFocusIE variable to track this:

    Hopefully this helps someone else searching for a way to focus on the original input field after selecting a date with the JQuery UI Datepicker control.

    2 Responses to JQuery UI Datepicker IE focus fix

    1. Radu says:
      June 22, 2012 at 9:55 am

      Just when I was losing hope…
      I need this for IE7 and your solution is the only one that I found to be working.
      Thanks Jeff!

      Reply
    2. Noam Lewis says:
      April 18, 2013 at 3:56 pm

      Thanks!

      I’ve made a revision to your code, to get it to work with jQuery 1.9.1 and jQuery-ui 1.10.1

      https://gist.github.com/sinelaw/5416130

      Reply

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Case Studies
  • Java
  • Grails
  • Mobile
  • Approach
  • People
  • Blog
  • Community
  • About
  • Careers
  • Leadership Team
  • Contact
  • Corporate Headquarters
    Object Partners, Inc.
    Butler Square, Suite 302A
    100 North Sixth Street
    Minneapolis, MN 55403
    (612) 746-1580