Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  137] [ 3]  / answers: 1 / hits: 17574  / 8 Years ago, sat, february 20, 2016, 12:00:00

Hi i want to Focus on div immediately after page load. it's working perfect on Firefox, but not on chrome, it's not working. this is my code :



https://jsfiddle.net/9yb2boxn/





document.getElementById(focusme).focus();

#focusme {width:400px; height:100px; overflow-y:scroll; }

<div id=focusme>
focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me focus me
</div>





in Firefox when i run this code and i Press Down Arrow on keyboard, the div scrolling down. but not on chrome. why this problem occure?



i already try



setTimeout(function() {

document.getElementById(focusme).focus();

}, 1);


still not working.



please help. thanks


More From » javascript

 Answers
-3

If you want to make a non-focusable element focusable, you must add a tabindex attribute to it:



<div id=focusme tabindex=1></div>


Also, you can use the ID to reach it with the location hash.
See the answer to your question here:
Is it possible to focus on a <div> using javascript focus() function?


[#63249] Wednesday, February 17, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yvettel

Total Points: 517
Total Questions: 101
Total Answers: 102

Location: Vanuatu
Member since Wed, Oct 14, 2020
4 Years ago
yvettel questions
Tue, Jul 27, 21, 00:00, 3 Years ago
Thu, Aug 13, 20, 00:00, 4 Years ago
Wed, Apr 15, 20, 00:00, 4 Years ago
Wed, Apr 1, 20, 00:00, 4 Years ago
;