title: Checking to see if a DOM element has focus
body: <p>I've got a lightbox textbox that is displayed using an AJAX call from an ASP.NET UpdatePanel. When the lightbox is displayed, I use the <code>focus()</code> method of a textbox that is in the lightbox to bring focus to the textbox right away.</p>
  
  <p>When in Firefox, the text box gains focus with no problem. In IE, the text box does not gain focus unless I use  </p>
  
  <pre><code>setTimeout(function(){txtBx.focus()}, 500);
  </code></pre>
  
  <p>to make the focus method fire slightly later, after the DOM element has been loaded I'm assuming.</p>
  
  <p>The problem is, immediately above that line, I'm already checking to see if the element is null/undefined, so the object already should exist if it hits that line, it just won't allow itself to gain focus right away for some reason.</p>
  
  <p>Obviously setting a timer to "fix" this problem isn't the best or most elegant way to solve this. I'd like to be able to do something like the following:  </p>
  
  <pre><code>var txtBx = document.getElementById('txtBx');
  
  if (txtPassword != null) {
      txtPassword.focus();
      while (txtPassword.focus === false) {
          txtPassword.focus();
      }
  }
  </code></pre>
  
  <p>Is there any way to tell that a text box has focus so I could do something like above?</p>
  
  <p>Or am I looking at this the wrong way?</p>
  
  <p><strong>Edit</strong><br>
  To clarify, I'm not calling the code on page load. The script <strong>is</strong> at the top of the page, however it is inside of a function that is called when ASP.NET's Asynchronous postback is complete, not when the page loads.</p>
  
  <p>Because this is displayed after an Ajax update, the DOM should already be loaded, so I'm assuming that jQuery's <code>$(document).ready()</code> event won't be helpful here.</p>