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
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.
Great information. Lucky me I discovered your website by accident (stumbleupon).
I’ve bookmarked it for later!
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
working fine
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.
Thanks alot man!
Thank, you. This works fine in IE 8 running in compatibility mode.
Thanks a lot bro.Its working fine
Thanks!
It’s works!!!
Thanks that worked like a charm
I wonder why lJS ibraries like JQuery can not implement this
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…
Thanx, using setTimeout is such a POG*
Thank you very much, this indeed helped! Without timeout only chrome set the focus right, now firefox and ie are working too!
Nice one! After a great search got this working!!
Awesome! It works wonders
Thank you!! IE is really strange browser!
Exelent. thanks
katu thanks for answer..
document.form1.textq.select();
document.form1.q.focus();
worked for me
Thanks Bro
It saves my time
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!
Thanks so much for the post, it really solved my big issue. Once again thanks.
The focus() twice worked for me for most instances.
Thank you! Fixed my IE problem.
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!
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
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 :-).