get focused element jquery code example
Example 1: jquery get current focus element
var $focused = $(':focus');
var focused = document.activeElement;
var hasFocus = $('foo').is(':focus');
elem === elem.ownerDocument.activeElement;
Example 2: how to get focused element in jquery
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>focus demo</title>
<style>
.focused {
background: #abcdef;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<div id="content">
<input tabIndex="1">
<input tabIndex="2">
<select tabIndex="3">
<option>select menu</option>
</select>
<div tabIndex="4">
a div
</div>
</div>
<script>
$( "#content" ).delegate( "*", "focus blur", function() {
var elem = $( this );
setTimeout(function() {
elem.toggleClass( "focused", elem.is( ":focus" ) );
}, 0 );
});
</script>
</body>
</html>