Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  135] [ 7]  / answers: 1 / hits: 49721  / 12 Years ago, sun, july 1, 2012, 12:00:00

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.


More From » jquery

 Answers
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>




[#84547] Friday, June 29, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zariahdiamondz

Total Points: 649
Total Questions: 109
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;