Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:47:51+00:00 2026-06-08T17:47:51+00:00

I have a textbox and a Jquery UI dialog. When a user is done

  • 0

I have a textbox and a Jquery UI dialog. When a user is done typing into the textbox and presses Enter, I want to display the dialog. In order to accomplish that I use the following code:

$(document).ready(function () {
    var helpDial = $('#helpDialog');
    var input = $('#helpSearch');

    helpDial.dialog({
        width: 800,
        autoOpen: false,
        modal: true,
        title: "Templates",
        position: [165, 158]
    });

    input.focusin(function () {
        helpDial.dialog('close');
    }).focusout(function () {
        helpDial.dialog('open');
    }).on('keypress', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            input.focusout();
            return false;
        }
    });
});

<input id="helpSearch" />
<div id="helpDialog"></div>

http://jsfiddle.net/7Fpm4/1

The problem is that when I press Enter, the focusout is called twice, one from the input.focusout() and the second time right after the helpDial.dialog('open') in the focusout event handler. This causes two background overlays to be created, and when I close the dialog, one overlay remains visible.

What am I doing wrong? Is there a better way to handle this scenario — ‘an enter pressed in a textfield opens a jQuery dialog’. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:47:52+00:00Added an answer on June 8, 2026 at 5:47 pm

    The only way to prevent an event from firing is to unbind the event handler. Or, depending on the situation, take differential action based on information available at the time the event fires.

    The focusout event is triggered any time the element loses the focus. In this case, every time the user clicks on the textbox, a focusin event is triggered. As soon as the cursor leaves the textbox (which happens when the dialog box is opened), the focusout event is triggered.

    The problem with your code is that you forcibly call the focusout event, and then the focusout event is called 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 is doing is binding a function to focusout within the focusin handler. If the user navigates away from the textbox, the focusout event handler will be called, which immediately unbinds the focusout event (to prevent binding the function multiple times). If the user hits the enter key, then the function is called manually, which removes the focusout event handler before opening the dialog to prevent triggering the focusout event from firing automatically when the dialog opens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have textbox that I want to run some jquery when the textbox loses
We have a textbox that we want to both trigger a jQuery UI datepicker,
I have setup a jQuery UI modal dialog to display when a user clicks
I've recently added the JQuery autocomplete plug in and have a textbox that autocompletes
I have a standard textbox and I've got jQuery on the page. I want
does jquery have any plugin that prevents entering any input to a textbox that
I have this code to prevent people entering '£' into a textbox jQuery(document).ready(function(){ jQuery('#cp_price').keypress(function(e){
I have written a simple jQuery dialog box that will appear in an asp
I have a view with a button that opens a jquery dialog via partial
From what I have played around with JQuery Dialog, I made an observation that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.