Pa3k.m
Pa3k.m

Reputation: 1156

How do I stop .focusout() from firing multiple times?

Here's a simple recreation of what Im trying to do:

https://jsfiddle.net/L9k4g2vw/1/

$('body').on('focusout', 'input', function(){
    alert('test');
});

<input type="text" name="textbox1" />
<input type="text" name="textbox2" />

When you focus out of the <input>, it runs fine, however, if you focus out of an <input> directly into another <input>, it creates a loop. What am I doing wrong? Thanks!

Upvotes: 1

Views: 2264

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074266

alert messes with focus. Your code is fine if you don't use alert (or the other old deprecated functions confirm, prompt, etc.; e.g., the 1990's style bring-the-world-to-a-stop functions):

$('body').on('focusout', 'input', function(){
    console.log('test');
});
<input type="text" name="textbox1" />
<input type="text" name="textbox2" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Jegadesh B S
Jegadesh B S

Reputation: 709

Hope this helps... I used JQuery....

$("<input />", {
  type: "number",
  min: "0"
}).addClass("someclass").appendTo($("#someid"));

$(document).on("focusout", ".someclass", function() {
  console.log("alert");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="someclass" />
<div id='someid'>

</div>

Upvotes: 2

Related Questions