Javascript DOM FocusEvent relatedTarget Property

Introduction

Get the related element of the element that triggered an onfocus event:

View in separate window

<!DOCTYPE html>
<html>
<body>

<textarea rows="4" cols="50">Click me to assure that I get focus. 
Then, click the input field to make sure that it gets focus.</textarea><br><br>
Input field: <input type="text" onfocus="getRelatedElement(event)">

<p id="demo"></p>


<script>
function getRelatedElement(event) {
  document.getElementById("demo").innerHTML = 
      "The related element that lost focus was: " + event.relatedTarget.tagName;
}
</script>/*from w  ww  .  ja va 2 s.  c om*/

</body>
</html>

The relatedTarget property returns the element related to the element that triggered the focus/blur event.

For onfocus and onfocusin events, the related element is the element that LOST focus.

For onblur and onfocusout events, the related element is the element that GOT focus.

This property is read-only.

The relatedTarget property returns null if there is no related element.




PreviousNext

Related