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.
Problem is that calling focus() works fine in IE6 and FF3.5, but not working in IE8.
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.
Question is: Has anybody faced similar problems and does anybody have any idea how to workaround it? Using setTimeout feels like very ugly workaround.
Tnx in advance
Edited : 26.08.2009
Succeeded to reproduce on simple example. Here is HTML+JS code that reproduces this bug on 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>
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.
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.
Hope this helps to understand where my problem is.
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 ;).
I had a similar problem with my app, but I can't reproduce the focus problem with your code. My problem was slightly different in that my page had a link hash that made IE not give my element focus.
Anyway, to get around the problem I added a timeout:
setTimeout(function () {
$('.my-thing').focus();
}, 100);
Not noticeable by a user but it gives IE a moment to breathe.
In conjunction with Kazys's solution, I found this to fix all my problems (using IE8 & .HTA files):
$("elem").blur();
$("elem").focus().focus();
I have no idea why, but somehow calling focus twice helps IE along.
EDIT:
I have found that calling .show() and .select() can also help.
Strangely i had the same problem and resolved it using plain old javascript like so:
document.getElementById('friend_name').focus();
Using jQuery equivalent $('#friend_name').focus(); didn't work in IE8 :-/
Had similar problem with IE8. I wanted to focus input in dialog when it is opened. Used autoOpen = false. Noticed that focus doesn't work only for first focusable element in dialog. Tried setTimeout but it worked only sometimes. Blurring element before focusing helped me.
$('#dialog').find('#name').blur();
$('#dialog').find('#name').focus();
Since you have not posted any code are you using:
$(document).ready(function(){
//code here
});
This will make javascript run after the html is loaded.
And you should use live events also. When your adding inputs to the dom the will automatically have focus binded to them.
$("p").live("focus", function(){
alert( $(this).text() );
});
This means that every p that is created will have a focus binded to it.
This is the best solution for the moment to set focus:
$('.elt').fadeIn(200, function() {$('.elt').focus();});
This is an old question, but it is top in search, so wanted to update.
I don't know if there was ever a time that it was fixed, but I ran into this issue again today in IE11, using jquery-2.1.3. I found that wrapping the focus call in a setTimeout, as set out by Ponny above, worked best for me.
I did need to increase the timeout in some cases, to get it to work.
Related
My friends, the code below used to work in Chrome till version 52.x but in the newest version it does not work anymore. Does any of you have any idea why it does not work?
https://jsfiddle.net/2jtrs2m2/
<input type="text" id="test" />
<script type="text/javascript">
var my_event = document.createEvent("TextEvent");
my_event.initTextEvent("textInput",true,false,window,"T");
document.getElementById("test").focus();
document.getElementById("test").dispatchEvent(my_event);
</script>
Usually when I executed this code, Chrome would insert the character T in the input. I dont want to use document.getElementById("xxx").value = "T" cause in some very specific cases I am working on, I really can only use the code above firing the oninput event which for some reason is not working anymore.
Try using document.execCommand
ie;
document.execCommand("insertText", false, "foo");
I have a very simple piece of javascript code that just should work and it only works when I run it in the browser console:
<script>
$(".hopscotch-close").click(function () {
alert("Hi");
Cookies.set("tourState", "closed")
})
</script>
Because it runs in the console I know that:
1) the ".hopscotch-close" is OK;
2) there are no errors in the code that could prevent it from running;
Also:
1) because is a "click" event I know that I haven't got a problem with the DOM being ready (and I can put everywhere - but in this case in at the bottom of the <body>;
2) I know I don't have an issue because of using the same name for a class than something else that exist;
3) The behavior is the same in Safari and Firefox, so its not a Browser issue.
I know this is tough without the full code, but if someone has experienced this maybe has na idea about what could be the problem.
From your comments I sense that the element is getting appended dynamically so instead of
$(".hopscotch-close").on('click',
you need to make use of event-delegation as
$(document).on('click','.hopscotch-close',function(){
That will do the trick.
If you are appending .hopscotch-close to any already existing static
element then instead of $(document).on('click' you can use
$('#yourStaticElementId').on('click','.hopscotch-close',function(){
which improves site performance.
I have a homepage that dynamically writes javascript in order to handle the mouseover of potential user choices. However, the .bind("mouseover",function()) does not seem to be working.
The PHP produces a script like this:
<script type="text/javascript">
function setPreview(art, title, rt, excerpt) {
$("#boxPreview").attr("src", art);
$("#selectedTitle").text(title);
$("#runningTime").text(rt);
$("#excerpt").text(excerpt);
}
$(document).ready(function() {
$("#tb0").bind("mouseover",setPreview(url,title,running time,excerpt));
$("#tb1").bind("mouseover",setPreview(url,title,running time,excerpt));
$("#tb2").bind("mouseover",setPreview(url,title,running time,excerpt));
$("#tb3").bind("mouseover",setPreview(url,title,running time,excerpt));
</script>
However, it seems that the mouseover event never fires. Instead, it seems that when the page is fully loaded, setPreview is run for the very last element (#tb3).
I have no idea what I'm doing wrong. If you would like to see the page in action for yourself, you can view it here.
You may try writing the same code like this
$("#tb0").bind("mouseover" , function(){
setPreview(url,title,running time,excerpt);
});
This may solve your issue. Because i've got same issue before but it was fixed writing this way.
It works fine in IE but does nothing in FF.
Here's the code I'm using it with.
The code does a some things for proboards.com.
I think the explanation of the code is irrelevant in this case. If not, let me know.
But the focus part is simply to put focus on a single element.
Here's the code:
<script>
var TitleBarGuestMessage="Hello Guest!";
var TitleBarMemberMessage="Welcome Back "+pb_displayname+"!";
var i,table,LTPI,LTPItable,titlerow,titlerowcell1,LTPIrow,LTPIrowcell1,shortcutA,shortcutB,shortcutC,
td=document.getElementsByTagName("td");LTPItable= document.getElementById("rectangle_table");LTPI=document.getElementById("rectangle_right_side");LTPItable.style.width="100%";
shortcutA=LTPI.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
shortcutB=shortcutA.previousSibling.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;shortcutC=shortcutA.firstChild;
shortcutA.previousSibling.parentNode.parentNode.cellSpacing="0";
shortcutA.previousSibling.style.display="none";
shortcutB.className="";
shortcutB.bgColor="transparent";
shortcutC.align="center";
shortcutC.style.backgroundColor="transparent";
shortcutC.className="";
LTPI.style.display="none";
for(i=0;i<td.length;i++){
if(td[i].className=="titlebg" && td[i].colSpan=="2"){
td[i].parentNode.parentNode.parentNode.id="forum";}}
table=document.getElementById("forum");
if(pb_action=="home"){
setTimeout(function(){table.focus();}, 2000)};
titlerow=table.insertRow(0);
titlerowcell1=titlerow.insertCell(0);
titlerowcell1.className="titlebg";
titlerowcell1.colSpan="5";
titlerowcell1.id="LTPI_titlebar";
LTPIrow=table.insertRow(1);
LTPIrowcell1=LTPIrow.insertCell(0);
LTPIrowcell1.colSpan="5";
LTPIrowcell1.innerHTML=LTPI.innerHTML;
LTPIrowcell1.id="LTPI_row";
if(pb_username=="Guest"){
titlerowcell1.innerHTML=TitleBarGuestMessage}
else{titlerowcell1.innerHTML=TitleBarMemberMessage}
</script>
This is the focus part of the script:
if(pb_action=="home"){
setTimeout(function(){table.focus();}, 2000)};
I have also tried:
setTimeout("table.focus();",2000);
Any suggestions?
this is because DOM table element does not have focus method (although it is implemented in IE). I assume that you need to scroll to that element, so you can do it this way:
window.scrollTo(0,table.scrollHeight);
Or as in your exact code:
if(pb_action=="home"){
setTimeout(function(){window.scrollTo(0,table.scrollHeight)}, 2000)};
Ok, I've looked through all the questions regarding this and I've tried several of the suggestions to no avail, so I'm hoping someone can shed more light on my problem.
OUTLINE OF THE ISSUE:
I'm running two Nivo sliders in a tabbed box. The code I have works in all the normal browsers, but some reason IE7 doesn't like the code I have and won't register the .click(function(e) when the tab is selected.
HERE IS THE CODE:
Part 1 - this loads the slider gallery on page load on the first tab:
<script type="text/javascript">
$(window).load(function() {
$('#slider').nivoSlider();
});
</script>
Part 2 - this is one IE7 has an issue with. This is for the other tabs so the gallery won't load until the tab is clicked. For some reason IE7 doesn't like this:
<script type="text/javascript">
$(document).ready(function(){
$('#gallery3-link').click(function(e){
$('#gallery1').nivoSlider();
return false;
});
});
</script>
THIS IS WHAT I'VE TRIED SO FAR:
I've tried using the $("#ClickMe").live('click', function() which didn't work as well as the $("body").delegate("p", "click", function() which were the two main solutions I saw people using to get this to work in IE7. When I was debugging I also set an alert to make sure IE was registering the click function:
$('#target').click(function() {
alert('Handler for .click() called.');
});
This had no effect. When you clicked on the tab, it didn't alert which confirmed the click function wasn't working. I've spent quite a while digging around for a solution to this and am plum out of resources. I thought it might something with the code, or some other work around - most of the sites I referenced were from circa 2006 or 2007. Not that JS has changed that much, but I was hoping maybe someone found a simplier solution in the last 4 years.
any help would greatly be appreciated.
D
Without seeing what you're actually working with, possibly you could try preventDefault() instead of return false;
<script type="text/javascript">
$(document).ready(function(){
$('#gallery3-link').click(function(e){
e.preventDefault();
$('#gallery1').nivoSlider();
});
});
</script>
I am guessing it is an error before that, that is causing the issue. Are there any errors on the page? Have you tried putting a simple alert('test') where the click function is set? If so, does it work?
EDIT:
From the other things you reference which I see when I did a search is the person was using IETester and it worked fine in regular IE7 and IE8. Are you using a real version of IE7?
The best solution I found was to simply load all the galleries on page load using the:
<script type="text/javascript">
$(window).load(function() {
$('#slider').nivoSlider();
});
</script>
It does add some time to the page load time - just about a 1/2 second more, but it solves the problem in IE7.
Thanks for everybody's help in this.
D
I just ran into this same bug, none of the other answers here were satisfactory. I solved this myself by using
$('body').click(function() { ... });