Main Tutorials

focus() is not working in IE ? – solution

The focus() method is used to give focus to a textbox, or other html components. Here is a simple example to make a textbox focus automatically after page load.


 

focus is not working in IE


 

The above code is working fine in Firefox (FF), but not in Internet Explorer (IE). Actually the IE is supporting the focus() method, we just need to use other way to write the focus method.

Base on my personal observation, this is because IE run the focus() method before the textbox render properly. However the Javascript is put after the textbox, the focus() method suppose to execute after textbox fully render itself, may be IE treat it as different ways? Ya, IE always have “IE-ONLY” implementation…

Here is an unofficial solution, we have to use the setTimeout() fucntion to delay the focus() execute time.

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

After delay the execute time, the focus() is working fine in IE , or even FF.


 

focus is working in IE


 

Reference

setTimeout() – http://www.w3schools.com/htmldom/met_win_settimeout.asp

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
27 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Chey
9 years ago

Do an onload.. and move your javascript right before the tag. then window.onload= function() { document.getElementById(‘myInput’).focus(); }

Everyone has different load time .. setting a timeout will not always work.

Doris
11 years ago

Great information. Lucky me I discovered your website by accident (stumbleupon).
I’ve bookmarked it for later!

Albert Golubev
5 years ago

I’m trying to run my selenium script on Windows 7 IE9. I’m using the following code to disable a none sticky header ((JavascriptExecutor)driver).executeScript(“const els=document.getElementsByClassName(‘survey-page’);els[0].style.display = ‘none’;”); . However I get an error, that Javascript couldn’t be executed. Works fine in Chrome , and firefox.

Thanks

DHAYANAND GOWDA Kalimidi
8 years ago

working fine

Sabin Subedi
8 years ago

I have a problem with IE. Although I use setTimeout() function cannot focus on other textbox on the same page. what is the problem. I can’t find the solution.

Tankful
10 years ago

Thanks alot man!

Jared Beach
10 years ago

Thank, you. This works fine in IE 8 running in compatibility mode.

Sathish
10 years ago

Thanks a lot bro.Its working fine

ali
10 years ago

Thanks!

A M
11 years ago

It’s works!!!

Abhi
11 years ago

Thanks that worked like a charm

I wonder why lJS ibraries like JQuery can not implement this

ramesh
11 years ago

setTimeout(function() { document.getElementById(‘myInput’).focus(); }, 1000); it works fine
but it’s focusing for every 1000 seconds to that element and it’s calling infinitely to focus the element..
It’s not the correct one.. becoz performance of an application degrades.. if we use infinite loop…

Daniel
11 years ago

Thanx, using setTimeout is such a POG*

Charles
11 years ago

Thank you very much, this indeed helped! Without timeout only chrome set the focus right, now firefox and ie are working too!

PRG
11 years ago

Nice one! After a great search got this working!!

Jack
11 years ago

Awesome! It works wonders

denis
11 years ago

Thank you!! IE is really strange browser!

empleo df
12 years ago

Exelent. thanks

kk
12 years ago

katu thanks for answer..

document.form1.textq.select();
document.form1.q.focus();

worked for me

sudhakar
12 years ago

Thanks Bro
It saves my time

Jared
13 years ago

Like Chris, setTimeout worked for me only about half of the time on IE, but calling focus() twice seems to work 100% of the time. Thanks much!

tanweer
13 years ago

Thanks so much for the post, it really solved my big issue. Once again thanks.

Dan
13 years ago

The focus() twice worked for me for most instances.

Chip
13 years ago

Thank you! Fixed my IE problem.

Erik
13 years ago

Chris: Your solution of calling focus() twice works like a charm, at least on IE 8 and Firefox.

I also had problems with field focus on Internet Explorer (8), and tried the timeout approach, which I couldn’t get to work reliably.

Thanks!

katu
13 years ago

This does not work for me, but I fixed the problem with
document.form1.textq.select();
document.form1.q.focus();

where textq is the id of the input text and q, the name of the input text.

i hope this is usefull for someone

Chris
14 years ago

Hi,

I recently had a similar problem, and Google lead me to your website. I tried your “setTimeout()” approach, but it didn’t behave reliably across my test machines. What I eventually did was call focus() *twice* instead:

var element = document.getElementById(‘myInput’);
element.focus();
element.focus();

Astonishingly, this give the field focus correctly in both IE6 and IE7. Firefox and Chrome only need focus() to be called once (of course) for the correct behaviour :-).