Is it possible to write onFocus/lostFocus handler for a DIV using JS or jQuery?
Asked Answered
E

4

13

I have a div and when the user clicks the div a function should be called. And when the user clicks something else (anything other than this div) another function should be called.

So basically i need to have onFocus() and lostFocus() function calls associated with this DIV. Is it available in JavaScript or even in jQuery?

Thanks.

Elfish answered 1/7, 2012 at 7:51 Comment(2)
why you need onFocus if you want to call function on Click event?Focus
@Mohammad, Yes, i could make use of Click event instead of onFocus. But the problem is, i need to have the lostFocus event or something similar to it. Any idea?Elfish
F
32

You need to add tabindex attribute to div :

$("#mydiv").focusin(function() {
  $("#mydiv").css("background", "red");
});
$("#mydiv").focusout(function() {
  $("#mydiv").css("background", "white");
});
#mydiv {
  width: 50px;
  height: 50px;
  border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mydiv" tabindex="100"></div>
<div id="anotherdiv"></div>
Focus answered 1/7, 2012 at 8:8 Comment(5)
@Mohammad, Jashwant Thanks a lot for that answer guys. I saw the jsfiddle demo. Exactly what i wanted. But SADLY, this method isnt working in my code. It says => $("#mydiv").focusin is not a function.Elfish
@MohammadAdil, It worked. Cheers! :) May i know what is the relevence of TabIndex attr in this? Just being curios.Elfish
This method requires the JS code to be put in the document ready section, right?Elfish
you can put that in ready itself or inside a function that is called from ready --------And --tabindex is used to define a sequence that you follow when you use the Tab key to navigate through a pageFocus
This is because a div doesn't have a tabindex by default, and to use focusin and focusout on an element ,it should have a tabindex,,that is why we are setting a tabindex to our div..Focus
C
6

Focus do not work on DIV : http://api.jquery.com/focus/

ALso good read: jquery : focus to div is not working

If you want to focus a div or anything normally can't be focused, set the tag's tabindex to -1 first.
eg: $("div#header").attr("tabindex",-1).focus();
Cannell answered 1/7, 2012 at 7:56 Comment(4)
Your method worked and i got focus() working. Now how can i implement lostFocus()? blur() isn't working.Elfish
@Orbb Glad it helped: here you should take the code for lost focus / focus out from here: #3089238 :)Cannell
@Jashwant MAGIC :)) hows it goin? Ah I will read your question latter and reply in more detail might be this week :PCannell
@Jashwant lol :) not greedy for points Just want to give a Quality to your question :)) great job in question though! <although ++ are always welcome and encouraging> :)Cannell
S
0

Not sure if this solution suits your web page, but you could use JQuery on() to attach a click event to the body element and delegate it to child div's. Then in your event handler, test the id attribute and handle as such; something like:

$(body).on("click", "div", function (evt) {
     if ($(this).attr("id") == "innerDiv") {
            handle inner div click here
     } else {
            hanlde out div click here
     {
}

Hope this helps...

Shaer answered 1/7, 2012 at 8:4 Comment(0)
S
-1

Sure.

$("#your-div-id").focusin(function() {
    // code here
});

$("#your-div-id").focusout(function() {
    // code here
});
Secrecy answered 1/7, 2012 at 7:56 Comment(1)
it didnt work. I tried it as you suggested. Then i tried it with bind/live since the div is generated dynamically. But nope! :(Elfish

© 2022 - 2024 — McMap. All rights reserved.