how can i change focus into particular div after any event via jquery?
Asked
Active
Viewed 8.3k times
42
-
Well you can use: `$('#element').focus()` but i'm not too sure what you mean, because this won't have any effect on DIVs. You can't "focus" a div really... it doesn't do anything. What are you trying to achieve? – Thomas Clayson Sep 23 '10 at 10:55
2 Answers
86
To make a div focusable, it needs to have a tabindex
attribute.
<div id='test' tabindex='1'></div>
Then, you can focus with e.g.
$('#something').click(function() {
$('#test').focus();
});

Anton Dozortsev
- 4,782
- 5
- 34
- 69

sje397
- 41,293
- 8
- 87
- 103
-
1
-
[This data](http://nemisj.com/focusable/) seems to indicate it should work in FF. Are you sure it's just not a display issue? – sje397 Sep 23 '10 at 11:08
-
@Loganphp - I tested Firefox 3.6.8 using [this example](http://jsfiddle.net/SNXk2/) and it worked fine. – sje397 Sep 23 '10 at 11:11
-
ya fine.Your example is working me also but i don't know why mine is not working – Sep 23 '10 at 11:27
-
4
-
1Thanks it worked for me. I've been looking for this solution for a long time. Appreciated. – Rupam Datta Aug 22 '13 at 07:22
-
yes it work. to get more clarification visit http://vijaylathiya.blogspot.in/2014/04/move-cursor-focus-to-div-element-using.html – Vijay Lathiya Apr 30 '17 at 04:28
-
You should use `tabindex="0"` otherwise your element would be the first thing tabbed to on a keyboard. – ESR Jun 14 '18 at 00:30
-
what if you want one action to focus on one div and a second action to focus on a different div? put tabindex=0 for both and it will go to correct one based on id? – AZ Chad Dec 03 '18 at 22:06
11
Use $(window).scrollTop()
It'll scroll the window to the particular item.
Here is the example
var scrollPos = $(".focusme").offset().top;
$(window).scrollTop(scrollPos);

Nishad Up
- 3,457
- 1
- 28
- 32
-
nice one. Any idea how to calculate the window size and subtract 45% of that amount so the scroll pos ends up somewhere in the centre? – Aurovrata Mar 05 '18 at 15:16
-
found the answer [here](https://stackoverflow.com/questions/4952533/jquery-position-div-in-middle-of-page#answer-4952569) :) – Aurovrata Mar 05 '18 at 15:21