document.activeelement returns body

Jigar Parekh picture Jigar Parekh · Jul 2, 2012 · Viewed 19.9k times · Source
<input id="text1" tabindex="1" onblur="invokeFunc()"/>
<input id="text2" tabindex="2" onblur="invokeFunc()"/>

function invokeFunc(){
   // ajax request 
   alert(document.activeElement); 
   // this returns body element in firefox, safari and chrome.
}

i am trying set focus onblur on text boxes with proper tabindex set.

when i invoke javascript function onblur and try to get document.activeelement then it always return me body element instead of active element where focus is.

Answer

ThiefMaster picture ThiefMaster · Jul 2, 2012

Between leaving the old element and entering the new element the active element is indeed the document/body itself.

Demo: http://jsfiddle.net/u3uNP/