jQuery Examples - focus( fn )
Description
The focus(fn) method binds a function to the focus event of each matched element.
Syntax
Here is the simple syntax to use this method −
selector.focus(fn)
Parameters
Here is the description of all the parameters used by this method −
fn − A function to bind to the event on each of the set of matched elements.
Example
Following is a simple example showing the usage of this method. Here it binds a function to the focus event of each matched element −
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("#result1").blur(function(){
alert("focus lost!");
});
});
function setFocus(){
$("#result1").focus();
}
function removeFocus(){
$("#result1").blur();
}
</script>
</head>
<body>
<form name = "sampleForm">
<label>Enter Name: </label><input type = "text" id = "result1" title="Enter Name" value = "" ></input><br/><br/>
<input type = "button" value = "Focus" onclick="setFocus();"/> <input type = "button" value = "Remove Focus" onclick="removeFocus();"/>
</form>
</body>
</html>
This will produce following result −
jqueryexamples_events.htm
Advertisements