-->

jQuery focus() Method

By: Bipin C Mathew 4 months, 2 weeks ago

The focus is a Jquery Event Method is used to focus on an element. The element get focused by mouse click or by "tab-navigating" to it. The focus method is often used in combination with the blur() method.

Syntax:

$(selector).focus() -Trigger the focus event for selected elements.

$(selector).focus(function) -Attach a function to the focus event.

Parameter: It accepts an optional parameter “function” which specifies the function to run when the focus event occurs.

Example:

<html> 

<head> 
	<style> 
		span { 
			display: none; 
		} 
		
		body { 
			width: 35%; 
			height: 50px; 
			border: 2px solid green; 
			padding: 35px; 
			margin: 10px; 
		} 
	</style> 
	<script src="https://code.jquery.com/jquery-1.10.2.js"> 
	</script> 
</head> 

<body> 
	<!-- this paragraph element get focused -->
	<p> 
		<input type="text"> <span>focused</span></p> 

	<!-- jQuery code to show working of this method -->
	<script> 
		$("input").focus(function() { 
			$(this).next("span").css("display", "inline"); 
		}); 
	</script> 

</body> 

</html> 

Comments

Let us talk!

We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.