Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google chrome causing infinite loop on textbox focus

I'm getting some strange happenings on google chrome. With the following code I'm getting an infinite number of alerts.

<input type="text" />

$('input[type="text"]').live('focus', function(event) {
    alert('in');
});

http://jsfiddle.net/XppG9/

Firefox and IE8 are both fine.

Why is this happening in chrome?

like image 347
ajbeaven Avatar asked Jul 29 '11 06:07

ajbeaven


3 Answers

I think, it's because after you close the dialog (alert box), the focus returns on the textbox, therefore, the function will fire again.

like image 157
dpp Avatar answered Oct 05 '22 02:10

dpp


I think it's because the the browser is sending focus from the alert to your text field every time you click the alert's "OK" button. You're probably not going to be popping up an alert (methinks) in the final version of your code, so this might not be an issue in the long run.

like image 40
Mark Carpenter Avatar answered Oct 05 '22 02:10

Mark Carpenter


The problem is that the alert() is stealing focus from the input box, and then restoring it when the dialog is closed. You can fix this by clearing focus from the input box before you show the alert.

Example: http://jsfiddle.net/XppG9/6/

like image 25
aroth Avatar answered Oct 05 '22 04:10

aroth