tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

:focus selector

focus selector

Description: Selects element if it is currently focused.

jQuery(':focus')

version added: 1.6

As with other pseudo-class selectors (those that begin with a ":"), it is recommended to precede :focus with a tag name or some other selector; otherwise, the universal selector ("*") is implied. In other words, the bare $(':focus') is equivalent to $('*:focus'). If you are looking for the currently focused element, $( document.activeElement ) will retrieve it without having to search the whole DOM tree.

Example

Adds the focused class to whatever element has focus
<!DOCTYPE html>
<html>
<head>
 
<style>
.focused {
    background
: #abcdef;
}
</style>
 
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
 
<input tabIndex="1">
<input tabIndex="2">
<select tabIndex="3">
   
<option>select menu</option>
</select>
<div tabIndex="4">
    a div
</div>

<script>
$
("*").live("focus blur", function(e) {
   
var el = $(this);
    setTimeout
(function() {
       el
.toggleClass("focused", el.is(":focus"));
   
}, 0);
});
</script>

</body>
</html>
The output of the code above will be:
Was this information helpful?
   

Comments