Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

jQuery: Display a message when the focus is removed from an element

jQuery Fundamental - I : Exercise-40

Display a message when the focus is removed from an element.

Note: Here object is similar to an Array of DOM Elements.

<body>
<p>PHP Tutorial</p><b>w3resource</b>
<button id="button1">Click to see the effect</button> 
</body>

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Display a message when the focus is removed from an  element.</title>
</head>
<body>
<form>
<input id="target" type="text" value="Field 1">
<input type="text" value="Field 2">
</form>
<p>Set and remove focus from field1
</p>
</body>
</html>

JavaScript Code :

$( "#target" ).blur(function() {
  console.log( "Remove focus from fields1!" );
});

Used Methods :

  • .blur() : Attach a handler to an event for the elements.

See the Pen jquery-fundamental-exercise-40 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Demonstrate how to handle click and double-click on a paragraph.
Next: Find all button inputs and mark them using jQuery.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.