EN
JavaScript - onfocusout event example
0
points
In this article, we would like to show you onfocusout
event example in JavaScript.
Quick solution:
var myElement = document.querySelector('#my-element');
myElement.addEventListener('focusout', function() {
console.log('onfocusout event occurred.');
});
or:
<input type="text" onfocusout="handleFocusout()">
Practical examples
There are three common ways how to use onfocusout
event:
- with event listener,
- with element attribute.
1. Event listener based example
In this section, we want to show how to use onfocusout
event on input
element via event listener mechanism.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<p>Click on the input field to set focus, then click outside it.</p>
<input type="text" id="my-input">
<script>
var myInput = document.querySelector('#my-input');
myInput.addEventListener('focusout', function() {
console.log('onfocusout event occurred.');
});
</script>
</body>
</html>
2. Attribute based example
In this section, we want to show how to use onfocus
event on input
element via attribute.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<p>Click on the input field to set focus, then click outside it.</p>
<input type="text" onfocusout="handleFocusout()">
<script>
function handleFocusout(){
console.log('onfocusout event occurred.');
}
</script>
</body>
</html>