Set background color of a <div> element when the <div> element or any children elements get focus:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//from w w w.j av a 2s. c o m <script> $(document).ready(function(){ $("div").focusin(function(){ $(this).css("background-color", "blue"); }); $("div").focusout(function(){ $(this).css("background-color", "yellow"); }); }); </script> </head> <body> <div style="border: 1px solid black;padding:10px;"> First name: <input type="text"><br> Last name: <input type="text"> </div> <p>Click an input field to get focus. Click outside an input field to lose focus.</p> </body> </html>
The focusin event occurs when an element or any children elements gets focus.
The focusin()
method runs a function when a focus event occurs on the element, or its children element.
The focusin()
method triggers if any child elements get focus.
$(selector).focusin(function)
Parameter | Optional | Description |
---|---|---|
function | Required. | function to run when the focusin event occurs. |