gene b.
gene b.

Reputation: 11994

jQuery Focus Event Keeps Alert Box in Infinite Loop (Chrome)

Does anyone know why displaying a simple alert box in the following focus() event handler never stops? I also tried focusin() with the same result. Is the event being fired multiple times in a loop? I am running in Chrome

$('input[type=text]').focus(function() {  // Also tried focusin(..)
  alert('Focus');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Input:
<input type="text" id="myinput"/>

Upvotes: 2

Views: 760

Answers (1)

you can do it this way. you define a global variable first. then you can act on it. Reason : For the reason that it is always chosen. When you press the OK button on the alertbox, it becomes input focus again. Therefore it enters into a continuous loop.

$('input[type=text]').focus(function() {  // Also tried focusin(..)
  alert('focus');
  $(this).blur();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Input:
<input type="text" id="myinput"/>

Also : Javascript: cross browser solution for selecting all text inside a textbox on focus

Upvotes: 1

Related Questions