6

Please don't throw stones at me because i'm total newbie at js and jquery. is it possible to focus a div? i just wanted to process events when div is clicked OR is focused and when we click outside the div. Something like that:

HTML:

<div id="focusedDiv"></div>

JS:

    $("#focusedDiv").focus(function () {
        ....
    });

    $("#focusedDiv").focusout(function () {
        ....        
    });

Thanks in advance!

user1178399
  • 1,028
  • 8
  • 17
  • 32
  • http://stackoverflow.com/a/3777600/1763929 – Vucko Jun 11 '13 at 10:58
  • Refer this Link ,I think this may help you http://stackoverflow.com/questions/3656467/is-it-possible-to-focus-on-a-div-using-javascript-focus-function – Bharu Jun 11 '13 at 11:12

1 Answers1

29

You have to set tabindex attribute:

http://jsfiddle.net/QAkGV/

<div id="focusedDiv" tabindex="-1"></div>

Or:

$("#focusedDiv").attr('tabindex',-1).focus(function () {
        ....
    });

For style, you could set outline CSS property too:

$("#focusedDiv").css('outline',0).attr('tabindex',-1).focus(function () {
        ....
    });
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • yes to focus on Div element 1st you need to set tabindex property -1 than it jQuery .focus work look at http://vijaylathiya.blogspot.in/2014/04/move-cursor-focus-to-div-element-using.html – Vijay Lathiya Dec 25 '14 at 17:36