When onblur occurs, how can I find out which element focus went *to*?

Posted by Michiel Borkent on Stack Overflow See other posts from Stack Overflow or by Michiel Borkent
Published on 2008-09-23T14:48:43Z Indexed on 2010/04/03 19:53 UTC
Read the original article Hit count: 390

Filed under:
|

Suppose I attach an onblur function to an html input box like this:

<input id="myInput" onblur="function() { ... }"></input>

Is there a way to get the ID of the element which caused the onblur event to fire (the element which was clicked) inside the function? How?

For example, suppose I have a span like this:

<span id="mySpan">Hello World</span>

If I click the span right after the input element has focus, the input element will lose its focus. How does the function know that it was mySpan that was clicked?

PS: If the onclick event of the span would occur before the onblur event of the input element my problem would be solved, because I could set some status value indicating a specific element had been clicked.

PPS: The background of this problem is that I want to trigger an Ajax.AutoCompleter control externally (from a clickable element) to show its suggestions, without the suggestions disappearing immediately because of the onblur event on the input element. So I want to check in the OnBlur function if one specific element has been clicked, and if so, ignore the blur event.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about events