title: jQuery focus() sometimes not working in IE8
body: <p>I am developing webapp using jQuery.
  I have functionality that adds new row of 3 input fields. After creating these DOM elements I want to focus one of input fields. I am doing it with calling jQuery focus() function on necessary input field.</p>
  
  <p>Problem is that calling focus() works fine in IE6 and FF3.5, but not working in IE8.</p>
  
  <p>I was trying to make simple working example of this problem for showing it here, but with stripped version of code focus() is working fine. So my guess was that DOM is not ready yet when I call focus() in IE8. For this I tried calling setTimeout('myFocus()',400). I had success and in some of cases focus was really working but still not always. Randomly it does not focus my input field.</p>
  
  <p>Question is: Has anybody faced similar problems and does anybody have any idea how to workaround it? Using setTimeout feels like very ugly workaround.</p>
  
  <p>Tnx in advance</p>
  
  <p>Edited : 26.08.2009</p>
  
  <p>Succeeded to reproduce on simple example. Here is HTML+JS code that reproduces this bug on IE8.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;script type="text/javascript" src="js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          function performChanged() {
            formChanged = true;
          }
  
          function handleChange() {
            var parentDiv = $('#container');
            newValue = $(html).html();
  
            parentDiv.html(newValue);
            $(".sel1",parentDiv).bind('change',handleChange);
            //alert('Uncomment this and after alert focus will be on input');
            $("input.cv_values",parentDiv).focus();
          }
  
          $(document).ready(function() {
            $('.trackChange').bind('change', handleChange);
          });
          var html = '&lt;div class=\"div1\"&gt;\n&lt;select class=\"sel1\" id=\"sel1\" name=\"sel1\"&gt;&lt;option value=\"\"&gt;&lt;\/option&gt;\n&lt;option value=\"11\"&gt;Select me to see problem&lt;\/option&gt;\n&lt;\/select&gt;\n\n\n&lt;input class=\"cv_values\" id=\"sel3\" name=\"sel3\" size=\"30\" type=\"text\" value=\"\" /&gt;&lt;br/&gt;Focus should in input field. With alert it is but without alert focus is not there&lt;/div&gt;';
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;select class="trackChange" onchange='performChanged();'&gt;
        &lt;option value=""&gt;&lt;/option&gt;
        &lt;option value="1" &gt;Select me to generate new inputs&lt;/option&gt;
      &lt;/select&gt;
  
      &lt;div id="container"&gt;&lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>To reproduce:
  1) select value from first dropdown. You will see that first time input is working
  2) select value from second dropdown. You will see that bug is reproduced.</p>
  
  <p>Then in code you can comment out line where it shows JS alert(). Strange thing is that if there is this alert() then after it focus is working fine.</p>
  
  <p>Hope this helps to understand where my problem is.</p>
  
  <p>P.S. I need my app to work this way - it is regenerating those inputs after selecting value from dropdown. This is simplified example of my app ;).</p>