Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPad HTML Focus

Tags:

html

jquery

ipad

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

like image 327
apchester Avatar asked May 12 '11 13:05

apchester


People also ask

What is input focus in HTML?

The :focus CSS pseudo-class represents an element (such as a form input) that has received focus. It is generally triggered when the user clicks or taps on an element or selects it with the keyboard's Tab key.

How do you add focus to an element?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.


1 Answers

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.

like image 172
halfpastfour.am Avatar answered Nov 12 '22 10:11

halfpastfour.am