The onfocus event occurs when the element come into focus. By focus, we mean that the cursor is at that element. For example, if the element was an input text box in a form, the cursor would appear inside the element and you could type and enter data into the box.
These six events are useful when dealing with forms and form elements:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>DevGuru XHTML onfocus and onblur Example</title>
</head>
<body>
<form>
Click back and forth between input text windows One and Two.
<br />
Alert messages will state who is in focus and who has lost focus (become blur).
<br />
One:
<input onfocus="JavaScript:alert('One is in focus')"
onblur="JavaScript:alert('One has lost focus')" />
<br />
Two:
<input onfocus="JavaScript:alert('Two is in focus')"
onblur="JavaScript:alert('Two has lost focus')" />
</form>
</body>
</html>In this example, alert messages advise which element is in focus and which has lost focus.