javascript - which approach is more correct to get element in document with focus? -


i have went through couple of questions asked related , have found 2 common approach.

  1. have global element , update attaching onfocus() event each of element.
  2. document.activeelement , have following code update element in case of old browser not support property

    var focusedelement; document.addeventlistener("focus", function(e) {     focusedelement = e.target; }, true); document.addeventlistener("blur", function(e) {     focusedelement = null; }, true); 

now question 1 more correct/easy/efficient approach of above two? why? all,

your solution 1 horribly inefficient. attaching event handler each , every (focusable) element on page when attach body itself? not correct/easy/efficient way sure. solution 2 looks pretty good.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -