The only way to prevent the event from firing is to untie the event handler. Or, depending on the situation, take differential actions based on the information available at the time the event occurs.
The focusout event is focusout anytime an element loses focus. In this case, every time the user clicks on the text field, the focusin event is focusin . As soon as the cursor leaves the text field (which occurs when the dialog box opens), the focusout event is focusout .
The problem with your code is that you force the focusout event to focusout , and then the focusout event focusout naturally when you open the dialog. So, change your code as follows:
$(document).ready(function () { var helpDial = $('#helpDialog'); var input = $('#helpSearch'); helpDial.dialog({ width: 800, autoOpen: false, modal: true, title: "Templates", position: [165, 158] }); input.on('focusin', function () { input.on("focusout", textboxFocusOut); }).on('keypress', function (e) { var code = (e.keyCode ? e.keyCode : e.which); if (code == 13) { textboxFocusOut(); } }); function textboxFocusOut() { input.off("focusout", textboxFocusOut); helpDial.dialog('open'); } });
http://jsfiddle.net/8L7EL/1/
What this code does is bind the function to focusout in the focusin handler . If the user moves away from the text field, the focusout event handler will be called, which will immediately cancel the focusout event (to prevent the function from binding several times). If the user presses the enter key, the function is called manually, which removes the focusout event handler before opening the dialog to prevent the focusout event from automatically triggering when the dialog opens.
source share