Javascript DOM onblur Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("blur",
       myScript);

This example uses the addEventListener() method to attach a "blur" event to an input element.

Write something in the input field, and then click outside the field to lose focus (blur).

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" id="fname">
<p id="demo"></p>
<script>
document.getElementById("fname").addEventListener("blur", myFunction);

function myFunction() {/*from   www.j  a va2  s .c  om*/
  document.getElementById("demo").innerHTML = "Input field lost focus.";
}
</script>

</body>
</html>
Bubbles:
Cancelable:
Event type:
Supported HTML tags:












No
No
FocusEvent
ALL HTML elements,
EXCEPT:
<base>,
<bdo>,
<br>,
<head>,
<html>,
<iframe>,
<meta>,
<param>,
<script>,
<style>, and
<title>



PreviousNext

Related