Set background color of a <div> element when the <div> element or any child elements loses focus:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//w w w .j a v a 2 s . 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 focusout event occurs when an element or its children element loses focus.
The focusout()
method runs a function when a focusout event occurs on the element, or its children elements.
The focusout()
method triggers if any child elements lose focus.
$(selector).focusout(function)
Parameter | Optional | Description |
---|---|---|
function | Optional. | the function to run when the focusout event occurs |