jQueryUI autocomplete not working with dialog and zIndex

38,984

Solution 1

Try setting the appendTo option to "#copy_dialog":

$(/** autocomplete-selector **/)
    .autocomplete("option", "appendTo", "#copy_dialog");

This option specifies which element the autocomplete menu is appended to. By appending the menu to the dialog, the menu should inherit the correct z-index.

Solution 2

appendTo: Which element the menu should be appended to. When the value is null, the parents of the input field will be checked for a class of "ui-front". If an element with the "ui-front" class is found, the menu will be appended to that element. Regardless of the value, if no element is found, the menu will be appended to the body.

This means that <div id="copy_dialog" class="ui-front"> will do the trick. No need to use the option appendTo, that did not work for me.

Solution 3

The 'appendTo' option does not always work.

Most egregiously, it will not display past the height of the dialog, but also, if you are using a 3rd party utility (e.g. DataTables editor) you do not always have control over when a dialog, an input, etc. are being created, when they are attached to the DOM, what IDs they have, etc.

This seems to always work:

$(selector).autocomplete({
    open: function(event, ui){
        var dialog = $(this).closest('.ui-dialog');
        if(dialog.length > 0){
            $('.ui-autocomplete.ui-front').zIndex(dialog.zIndex()+1);
        }
    }
});

Solution 4

When using jQuery UI 1.10, you should not mess around with z-indexes (http://jqueryui.com/upgrade-guide/1.10/#removed-stack-option). The appendTo option works, but will limit the display to the height of the dialog.

To fix it: make sure the autocomplete element is in the correct DOM order with: autocomplete.insertAfter(dialog.parent())

Example

 var autoComplete,
     dlg = $("#copy_dialog"),
     input = $(".title", dlg);

 // initialize autocomplete
 input.autocomplete({
     ...
 });

 // get reference to autocomplete element
 autoComplete = input.autocomplete("widget");

 // init the dialog containing the input field
 dlg.dialog({
      ...
 });

 // move the autocomplete element after the dialog in the DOM
 autoComplete.insertAfter(dlg.parent());

Update for z-index problem after dialog click

The z-index of the autocomplete seems to change after a click on the dialog (as reported by MatteoC). The workaround below seems to fix this:

See fiddle: https://jsfiddle.net/sv9L7cnr/

// initialize autocomplete
input.autocomplete({
    source: ...,
    open: function () {
        autoComplete.zIndex(dlg.zIndex()+1);
    }
});

Solution 5

I recall having a similar issue with autocomplete and zIndex, and had to fix it by specifying the appendTo option: $(selector).autocomplete({appendTo: "#copy_dialog"})

This is also useful if you have an autocomplete inside a positioned element. The specific problem I had was an autocomplete inside a fixed position element that stayed in place while the main body scrolled. The autocomplete was displaying correctly but then scrolled with the body rather than staying fixed.

Share:
38,984
Francis Lewis
Author by

Francis Lewis

Updated on July 10, 2022

Comments

  • Francis Lewis
    Francis Lewis almost 2 years

    I ran into an interesting issue with jQueryUI autocomplete in a dialog box.

    My dialog HTML looks like this:

    <div id="copy_dialog">
        <table>
            <tbody>
                <tr>
                    <th>Title:</th>
                    <td><input type="text" class="title" name="title"></td>
                </tr>
                <tr>
                    <th>Number:</th>
                    <td><input type="text" name="number"></td>
                </tr>
            </tbody>
        </table>
    </div>
    

    When I run the jQueryUI autocomplete on the above HTML, it works perfect.

    When I open it up using dialog

    $('#copy').click(function()
    {
        $('#copy_dialog').dialog({
            autoOpen: true,
            width: 500,
            modal: false,
            zIndex: 10000000,
            title: 'Duplicate',
            buttons: {
                'Cancel': function()
                {
                    $(this).dialog('close');
                },
                'Save': function()
                {
                    $(this).dialog('close');
                }
            }
        });
    
        return false;
    });
    

    Then in FireBug, I can see autocomplete is still working. It's requesting and receiving results, but I no longer see a list of options below the input field.

    My thought is that this has something to do with the zIndex on the dialog box being much greater than what the autocomplete menu gives, but I don't know for sure. I'm still researching exact details of what's happening, but I'm hoping someone here will have some idea for me.

    Edit I tried removing the zIndex from the dialog and my autocomplete starts showing up. Unfortunately, I need that zIndex value to get over the dreadfully high zIndex of the menubar, which I can't change (don't have access to that area of the code). So if there's a way to add a zIndex to the autocomplete, that would be fantastic; until then, I'll probably just remove the zIndex from the dialog and make sure it doesn't show up around the menubar area.

  • salonMonsters
    salonMonsters about 11 years
    This doesn't work when the returned list is longer than the height of the modal dialog in which case the items only show up on top of the dialog and once they pass the bottom of it they are all hidden.
  • Thant Zin
    Thant Zin almost 11 years
    dropdown items coming out, but encounter another issues. can't choose items using mouse or keyboard as they hide away itself.
  • Tom
    Tom almost 11 years
    Thanks for updating this. Works like a charm. Follow up issue exits (stackoverflow.com/questions/17242943/…) but it was exactly what I needed.
  • Jay Rizzi
    Jay Rizzi almost 11 years
    this works on the inital dialog open, but subsequent dialog opens will again place the autocomplete behind the modal dialog
  • mhu
    mhu over 10 years
    You should call autoComplete.insertAfter(dlg.parent()); every time you show the dialog.
  • TimSPQR
    TimSPQR over 10 years
    Old thread. Late response. Worked great for me...I thank you.
  • Daniel
    Daniel over 10 years
    Using Jquery UI 1.10 I figured out, that you should use the appendTo option, passing the Dialog, and you should create the autocomplete only after you create the Dialog.
  • prograhammer
    prograhammer about 9 years
    Almost perfect...just needs to also be called in the close method as well. See my answer below.
  • mike123
    mike123 over 8 years
    @salonMonsters I'm experiencing exactly what you describe ... have you found a work-around?
  • Matteo C
    Matteo C over 8 years
    After trying ~10 fixes for this infuriating jQuery UI quirk which the developers refuse to call a "bug", this is the closest thing I've found to a working solution. I'm on 1.11.4. This works, except that if I click anywhere in the dialog while there are autocomplete options visible, they're forever after displayed underneath the dialog. I realize I'm complaining about free software, but I can't understand the developers' stubbornness regarding this. Autocomplete options should always be shown on top of anything else in the DOM. Period.
  • catorda
    catorda about 8 years
    Do you know why you have to add the appendTo option in the close event?
  • Zack Marrapese
    Zack Marrapese about 8 years
    Because the dialog always updates its z-index, this is the correct answer for me.
  • Brian Tompsett - 汤莱恩
    Brian Tompsett - 汤莱恩 almost 8 years
    Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge.
  • Andrea Mauro
    Andrea Mauro almost 5 years
    imho this is the best solution because it works anywhere, a better solution would be to implement this function by default in all autocomplete elements
  • Joe Johnston
    Joe Johnston about 4 years
    I thought the close event a trivia. Nay Nay! Do it! And then mark the answer up after you do. This works consistently for model and non-model forms with ajax sources if that matters to your case.