Javascript DOM onfocus Event

Introduction

Execute a JavaScript when an input field gets focus:

When the input field gets focus, a function is triggered which changes the background-color.

View in separate window

<!DOCTYPE html>
<html>
<body>

Enter your name: <input type="text" onfocus="myFunction(this)">
<script>
function myFunction(x) {//from   w ww  .  j  ava2s .  c  om
  x.style.background = "yellow";
}
</script>

</body>
</html>

The onfocus event occurs when an element gets focus.

The onfocus event is most often used with <input>, <select>, and <a>.

The onfocus event is the opposite of the onblur event.

The onfocus event is similar to the onfocusin event.

The main difference is that the onfocus event does not bubble.

To find out whether an element or its child gets the focus, use the onfocusin event.

You can achieve this by using the optional useCapture parameter of the addEventListener() method for the onfocus event.

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