42

how can i change focus into particular div after any event via jquery?

1
  • 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? Sep 23, 2010 at 10:55

2 Answers 2

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();
});
9
  • 1
    It is fine with IE and Chrome.It is not working in Firefox.What can i do?
    – Loganphp
    Sep 23, 2010 at 11:02
  • This data seems to indicate it should work in FF. Are you sure it's just not a display issue?
    – sje397
    Sep 23, 2010 at 11:08
  • @Loganphp - I tested Firefox 3.6.8 using this example and it worked fine.
    – sje397
    Sep 23, 2010 at 11:11
  • 4
    tabindex to focus on non form elements, cool! Never new, thank you!
    – Jekis
    May 5, 2013 at 14:54
  • 1
    Thanks it worked for me. I've been looking for this solution for a long time. Appreciated. Aug 22, 2013 at 7:22
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);
2
  • 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 5, 2018 at 15:16
  • found the answer here :)
    – Aurovrata
    Mar 5, 2018 at 15:21