FocusEvent relatedTarget Property - Javascript DOM

Javascript examples for DOM:Focus Event

Description

The relatedTarget property returns the element related to the focus event.

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

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

This property is read-only.

Return Value

A reference to the related element, or null if there is no related element

The following code shows how to get the related element an onfocus event.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<textarea rows="4" cols="50">Click me. Then, click the input field.</textarea><br><br>

Input field: <input type="text" onfocus="getRelatedElement(event)">

<script>
function getRelatedElement(event) {
    console.log("The related element that lost focus was: " + event.relatedTarget.tagName);
}
</script>/*from ww  w.  ja v a 2  s. co m*/

</body>
</html>

Related Tutorials