Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

focus() not working within setTimeout on iOS

Tags:

jquery

css

focus

jQuery's focus() method is does not appear to work when used from within a setTimeout in iOS.

So,

setTimeout( function () {
    // Appears to have no effect in iOS, fine in Chrome/Safari/Firefox/IE
    $('.search').focus();    

}, 500);

But on it's own,

// works fine.
$('.search').focus();    

See the following example:

http://jsfiddle.net/nwe44/ypjkH/1/

If the focus() call is made outside the setTimeout it works, inside it doesn't. This is doubly curious as other methods do work. For example, in my jsFiddle I'm able to change the border color, just not focus it. Any ideas?

like image 210
Nicholas Evans Avatar asked Jul 28 '11 22:07

Nicholas Evans


1 Answers

Check fiddle i have updated at http://jsfiddle.net/ypjkH/7/

    $('#selector').click( function (e) {
        e.preventDefault();
        setTimeout( doFocus  
    , 3000);

    });

  function doFocus() {
      $('.search').focus().css('border', '1px solid red');
  }
like image 189
Ehtesham Avatar answered Sep 19 '22 13:09

Ehtesham