onblur Event blur Javascript DOM Tutorial Share / Like
The blur event fires when a target element loses focus, meaning that the user has stopped interacting with the target element and has moved on to interacting with something else on the page.
Scripting blur events through Javascript and the DOM:
Javascript CODE EXAMPLE
<script type="text/javascript">
function addEventsToHTML(){
var field1 = document.getElementById('field1');
var field2 = document.getElementById('field2');
field1.onblur = lostFocus;
field2.onblur = lostFocus;
function lostFocus(){
alert(this.id+" just lost focus.");
}
}
window.onload = addEventsToHTML;
</script>
<input id="field1">
<input id="field2">
Establishing blur events direct on the html elements:
Javascript CODE EXAMPLE
<script type="text/javascript">
function lostFocus(target){
alert(target.id+" just lost focus.");
}
window.onload = addEventsToHTML;
</script>
<input onblur="lostFocus(this);" id="field1">
<input onblur="lostFocus(this);" id="field2">