iPad HTML Focus

17,712

Unfortunately, focusing is not possible with the iPad. The only way the focus will be placed in the desired field is when the user 'clicks' on the desired field.

Update 2015-01-08:

As explained in this answer by Matt:

... iOS will only allow focus to be triggered on other elements, from within a function, if the first function in the call stack was triggered by a non-programmatic event. In your case, the call to setTimeout starts a new call stack, and the security mechanism kicks in to prevent you from setting focus on the input.

Share:
17,712
apchester
Author by

apchester

Love to program in ruby and objective-C, mostly with rails and Cocoa.

Updated on August 07, 2022

Comments

  • apchester
    apchester over 1 year

    I have an issue with focusing a text field on the iPad. Where I use:

    $(document).bind("click",function(event) {
        alert('click');
        focusTextArea();
    });
    

    The focus is set to the text area, and a keyboard appears. However when called with touchend, focusTextArea is not called and the keyboard is not made visible. My focusTextArea function is:

    function focusTextArea() {
        $('#textArea').focus();
    }
    

    Does anybody know why this occurs, and how I might be able to get this to work?

    TIA,

    Adam

    • FarFigNewton
      FarFigNewton almost 13 years
      Are you using the jquery plugin touchend?
    • Tim
      Tim almost 13 years
      Can you elaborate on what you meant by "when called with touchend"?
    • apchester
      apchester almost 13 years
      @guanome- not using any plugins, just standard jquery.
    • apchester
      apchester almost 13 years
      @tim, Sorry I meant replacing the click event argument of bind with the touchend event.
    • Kheldar
      Kheldar over 12 years
    • Ads
      Ads over 10 years
      I got around this by adding a div that covered the whole display area with some basic instruction in the middle "Tap to Begin". Then bound a "tap" event to the div. The tap then hid the div and set focus to the textbox. That was the only way I could find to get around this crazy problem.
  • robocat
    robocat almost 12 years
    Additionally, in an onclick event for any element, you can call focus() to another input.
  • Jörn Berkefeld
    Jörn Berkefeld about 11 years
    not completely true: if the focus is triggered within the callback of a click event on another INPUT field, this actually works (unless you have an animation in between which seems to interrupt this option again...). so yes it does kinda (not) work, sort of :P
  • ddlab
    ddlab about 10 years
    Unfortunately, 'touchend' doesn't work in every case, but 'mouseup' does ! Please tryout and find your best practice.
  • Daft
    Daft over 9 years
    Any references to support that Bob? I know myself first hand that focus doesn't work properly. I'm just wondering if Apple has said anything about it themselves.