Focus Doesn't Work in Ie

focus doesn't work in IE

For IE you need to use a settimeout function due to it being lazy, for example:

setTimeout(function() { document.getElementById('myInput').focus(); }, 10);

From http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/

For opera, this may help:
how to set focus in required index on textbox for opera

UPDATE:

The following snippet of code handles the case when the element is unavailable and retries after a short period - perfect for slow loading pages and/or elements not available until some time after.

setTimeout(

function( ) {

var el = document.getElementById( "myInput" ) ;
( el != null ) ? el.focus( ) : setTimeout( arguments.callee , 10 ) ;

}

, 10 ) ;

Focus is not working in IE even after adding much delay

https://stackoverflow.com/a/2600261/5086633 is for IE7.

Following works each time on all browsers.

<body onload="document.getElementById('testfocus').focus();">

You can see if the favicon shows a rotating circle(loading) as the focus will happen only when the loading is complete.

I tried with following which is working on all browsers:

<html>
<head></head>
<body onload="document.getElementById('testfocus').focus();">
<iframe src="http://www.healthcarereformdigest.com/wp-content/uploads/2014/07/forms.jpg"></iframe>
<input id="testfocus"/>
</body>
</html>

(Added iFrame and that image as its heavy and it takes looong to load).

If its failing in your app, you can press Tab to see which element has the focus and then Inspect that as it might have multiple focus() statements.

Unable to focus an input using JavaScript in IE11

The issue was focusing in IE11 is broken when the css property -ms-user-select: none is applied to the input. So by changing:

* {
-ms-user-select: none;
}

into

*:not(input) {
-ms-user-select: none;
}

I was able to solve the problem. Here is a codepen for reproducing the issue:
http://codepen.io/anon/pen/yNrJZz

autofocus attribute on input field not working in IE 11

IE11 doesn`t support autofocus (despite is in caniuse).
You can check How to make input autofocus in internet explorer?



Related Topics



Leave a reply



Submit