Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hmm... In Firefox, you can use <code>explicitOriginalTarget</code> to pull the element that was clicked on. I expected <code>toElement</code> to do the same for IE, but it does not appear to work... However, you can pull the newly-focused element from the document:</p> <pre><code>function showBlur(ev) { var target = ev.explicitOriginalTarget||document.activeElement; document.getElementById("focused").value = target ? target.id||target.tagName||target : ''; } ... &lt;button id="btn1" onblur="showBlur(event)"&gt;Button 1&lt;/button&gt; &lt;button id="btn2" onblur="showBlur(event)"&gt;Button 2&lt;/button&gt; &lt;button id="btn3" onblur="showBlur(event)"&gt;Button 3&lt;/button&gt; &lt;input id="focused" type="text" disabled="disabled" /&gt; </code></pre> <hr> <p><strong>Caveat:</strong> This technique does <em>not</em> work for focus changes caused by <em>tabbing</em> through fields with the keyboard, and does not work at all in Chrome or Safari. The big problem with using <code>activeElement</code> (except in IE) is that it is not consistently updated until <em>after</em> the <code>blur</code> event has been processed, and may have no valid value at all during processing! This can be mitigated with a variation on <a href="https://stackoverflow.com/questions/121499/when-onblur-occurs-how-can-i-find-out-which-element-focus-went-to/128452#128452">the technique Michiel ended up using</a>:</p> <pre><code>function showBlur(ev) { // Use timeout to delay examination of activeElement until after blur/focus // events have been processed. setTimeout(function() { var target = document.activeElement; document.getElementById("focused").value = target ? target.id||target.tagName||target : ''; }, 1); } </code></pre> <p>This should work in most modern browsers (tested in Chrome, IE, and Firefox), with the caveat that Chrome does not set focus on buttons that are <em>clicked</em> (vs. tabbed to). </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload