I am developing a webapp using jQuery. I have functionality that adds a new line of three input fields. After creating these DOM elements, I want to focus one of the input fields. I do this with a jQuery focus () function call in the desired input field.
The problem is that the focus () call works fine in IE6 and FF3.5, but doesn't work in IE8.
I tried to make a simple working example of this problem to show it here, but with the split version of the code focus () works fine. So I assumed that the DOM is not ready yet when I call focus () in IE8. To do this, I tried calling setTimeout ('myFocus ()', 400). I had success, and in some cases the focus really worked, but still not always. By chance, he does not focus my input field.
Question: Has anyone encountered similar problems, and does anyone know how to deal with him? Using setTimeout seems like a very ugly workaround.
Tnx in advance
Edited: 08/26/2009
Succeed for playback with a simple example. Here is the HTML + JS code that reproduces this error in IE8.
<html> <head> <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript"> 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 = '<div class=\"div1\">\n<select class=\"sel1\" id=\"sel1\" name=\"sel1\"><option value=\"\"><\/option>\n<option value=\"11\">Select me to see problem<\/option>\n<\/select>\n\n\n<input class=\"cv_values\" id=\"sel3\" name=\"sel3\" size=\"30\" type=\"text\" value=\"\" /><br/>Focus should in input field. With alert it is but without alert focus is not there</div>'; </script> </head> <body> <select class="trackChange" onchange='performChanged();'> <option value=""></option> <option value="1" >Select me to generate new inputs</option> </select> <div id="container"></div> </body>
Playback: 1) select a value from the first drop-down list. You will see that the first input is working 2) select a value from the second drop-down list. You will see that the error is reproducing.
Then in the code you can comment out the line where JS alert () is displayed. The strange thing is that if there is this warning (), then after the focus is working fine.
Hope this helps to understand where my problem is.
PS I need my application to work this way - it restores these inputs after selecting a value from the drop-down list. This is a simplified example of my application;).
javascript jquery internet-explorer-8
oskarae Aug 25 '09 at 9:00 2009-08-25 09:00
source share