4

After adding text in the input fields and clicking on the button is not triggering click event. But clicking on the button triggers the click event.

$("#title").focusout(function() {
  alert('foucus out');
});

$("#ok").click(function() {
  alert('click');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Title
<input id="title" val="hello" />
<input id="ok" type="button" value="OK" />
Barmar
  • 741,623
  • 53
  • 500
  • 612
MRK
  • 61
  • 1
  • 3
  • After typing text into the input box then clicking the button I get 2 alert boxes, in the order I would expect. You may want to check your error console to make sure it's not throwing an error in your browser. – Brian Shamblen Apr 28 '15 at 00:05
  • In chrome I get the behavior described: either a focus out alert if focus was originally in the text box, or a click alert otherwise. In FireFox I never get the focusout alert, I only get click alerts. – Barmar Apr 28 '15 at 00:11
  • check this out, maybe this can help http://stackoverflow.com/questions/4084780/how-should-i-fire-javascript-blur-event-after-click-event-that-causes-the-blur – Robin Carlo Catacutan Apr 28 '15 at 00:42
  • you can use $("#title").on('blur',function(){}); instead of focusout – Mohamed-Yousef Apr 28 '15 at 01:04
  • Setting a timer that delays the blur event action before it fires the button click event helps $("#title").focusout(function() { setTimeout(function() { alert('foucus out'); }, 500); }); – MRK Apr 28 '15 at 11:06

1 Answers1

2

Setting a timer that delays the blur action fires the click event.

$("#title").focusout(function() {
    setTimeout(function() {
      alert('foucus out');
    }, 500);
});

$("#ok").click(function() {
  alert('click');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Title
<input id="title" val="hello" />
<input id="ok" type="button" value="OK" />
MRK
  • 61
  • 1
  • 3
  • it is not a good solution because let suppose i want to add validatoin on event "focusout" and then want to submit. But If the focusout event will run after five secongs and form have been submitted then how is that possible to add validation. I think is not good solution Thanks – Techleadz Team May 24 '19 at 18:33