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 the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
DHAYANAND GOWDA Kalimidi
Guest
DHAYANAND GOWDA Kalimidi

working fine

Sabin Subedi
Guest
Sabin Subedi

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.

Chey
Guest
Chey

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.

Tankful
Guest
Tankful

Thanks alot man!

Jared Beach
Guest
Jared Beach

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

trackback
Javascript: Trouble getting window to focus() in IE | Technology & Programming Answers

[…] focuses fine for FireFox. It stays in the background in Internet Explorer. I’ve tried the common fix to no […]

trackback
IE7?8?focus()?????? | ?????

[…] ??URL Focus() Is Not Working In IE ? – Solution […]

Sathish
Guest
Sathish

Thanks a lot bro.Its working fine

ali
Guest
ali

Thanks!

A M
Guest
A M

It’s works!!!

Doris
Guest
Doris

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

Abhi
Guest
Abhi

Thanks that worked like a charm

I wonder why lJS ibraries like JQuery can not implement this

ramesh
Guest
ramesh

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
Guest
Daniel

Thanx, using setTimeout is such a POG*

Charles
Guest
Charles

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

trackback
Power of YUTORI » IE7,8?focus()?????
PRG
Guest
PRG

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

trackback
focus doesn't work in IE | Easy jQuery | Free Popular Tips Tricks Plugins API Javascript and Themes
Jack
Guest
Jack

Awesome! It works wonders

denis
Guest
denis

Thank you!! IE is really strange browser!

empleo df
Guest
empleo df

Exelent. thanks

kk
Guest
kk

katu thanks for answer..

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

worked for me

sudhakar
Guest
sudhakar

Thanks Bro
It saves my time

Jared
Guest
Jared

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
Guest
tanweer

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

Dan
Guest
Dan

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

Chip
Guest
Chip

Thank you! Fixed my IE problem.

Erik
Guest
Erik

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
Guest
katu

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
Guest
Chris

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 :-).