Javascript DOM onfocus Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("focus",
       myScript);

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

View in separate window

<!DOCTYPE html>
<html>
<body>
Enter your name: <input type="text" id="fname">

<script>
document.getElementById("fname").addEventListener("focus", myFunction);

function myFunction() {//  w  w  w  .j av  a  2 s. c  o  m
  document.getElementById("fname").style.backgroundColor = "red";
}
</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