jQuery UI Autocomplete - menu disappears on hover
Asked Answered
F

6

61

I'm trying to use jQuery UI Autocomplete to bring up a list of names of people in a MySQL Database. The basic function is working - when you type in two or more letters a list of possible names is displayed - but when I hover the mouse over the list or press the down key to access the list, it disappears (2 screenshots below may help to explain this).

What this means is that the Autocomplete becomes pointless as I can't actually access the list! If anyone can help I'd be most appreciative! Screenshots and code are posted below.

Type in the first few characters and the menu appears

Screenshot 1

But hover the mouse or press the 'down' key and it disappears before a selection can be made

Screenshot 2

HTML:

  <div id="chooseaccount">
  Choose Account to Edit
  <div id="iechooseaccountlabel" class="labeldiv">
    <!--[if IE]>
     Enter Student Name
    <![endif]-->
   </div>

  <input type="text" class="inputs" id="editname" placeholder="Enter Student Name" />

  </div>

jQuery:

$(document).ready(function(){

$("#editname").autocomplete({
 source: "names.php",
 minLength: 2,
});

});

PHP:

<?php  

$mysqli = new mysqli('********', '********', '**********', '*********');
$text = $mysqli->real_escape_string($_GET['term']);

$query = "SELECT name FROM users WHERE name LIKE '%$text%' ORDER BY name ASC";
$result = $mysqli->query($query);
$json = '[';
$first = true;
while($row = $result->fetch_assoc())
{
if (!$first) { $json .=  ','; } else { $first = false; }
$json .= '{"value":"'.$row['name'].'"}';
}
$json .= ']';
echo $json;

?>  
Fabri answered 7/2, 2013 at 9:56 Comment(4)
Well it has to work as shown in source - jqueryui.com/autocomplete . Have you edited the .js file? Or try using or including a fresh copy of it.Erastes
Haven't touched the .js file and have tried including a fresh copy but still having the same problem. The only thing I have edited is the .css file but haven't touched anything to do with :hoverFabri
im haveing the same issue with jquery ui 1.10.0... fresh install, code taken from example, Could it be in 1.10 autocomplete is broken?Heroine
@Chris, did you manage to fix it? I have the same issue here.Jacksmelt
S
203

The error is caused because of a conflict that occurs when two jQuery UI files are loaded into the client's browser at the same time. If you peak into your <head> section you will probably see you have two different jQuery UI files referenced there. Just remove one and it will work.

Simasimah answered 26/2, 2013 at 12:14 Comment(4)
same solution worked for me also.. I had a datepicker.js file which was a subset of jquery.ui and I included jquery also.. I excluded datepicker.js and it worked fine..Deprecatory
Check out the new jQuery combobox autocomplete http://jqueryui.com/autocomplete/#comboboxMorvin
Hi, for everyone else having problem locating the duplicate file, the problem is also caused by the library being referenced twice. If you already have the jquery-ui-1.10.x.js, drop the individual libraries. Because the jquery-ui.js already contains all libraries (including your jquery.ui.autocomplete.js) https://mcmap.net/q/323861/-jquery-ui-combobox-autocomplete-disappearsJaysonjaywalk
I can tell you that I had a similar issue in general (the results of the "autocomplete" feature disappeared when I hovered with the mouse), so I've found out that I had both jQuery UI in a bundle file AND JQUI with the autocomplete feature. So I downloaded the bundle file with the autocomplete feature and removed the other file from the includes. Thanks for this answer that got me into the right direction to resolve the issue in question.Batts
P
5

I had the same problem, but I only ever had one jquery-ui script tag in the DOM at a time. I was loading content with Ajax that included the script tag. If I did that twice on one page, it would break the autocomplete dropdown, even though the content of the second request was replacing the content of the first. One workaround is to add this line before rendering content containing the jquery-ui script:

$.ui = null;

Prussian answered 21/4, 2015 at 22:49 Comment(2)
It's better to just remove one of the jquery-ui scripts. It acually does'nt make any sense at all to load it twiceHillegass
@Hillegass I agree. In this case one script tag was essentially replacing another, so there was only one script tag in the DOM but it was loading a second time. I had to use this workaround because I didn't have control over the content that I was fetching.Prussian
N
4

This error is caused when two jQuery UI files are loaded into your browser at the same time.This may cause jquery conflict. Remove the unused jquery UI file to solve the error.

In my case jquery-ui.min.js file was unintentionally included from assets folder.To remove it i added one code in components in config/main.php

     'clientScript' => array(
        'scriptMap' => array(
            'jquery-ui.min.js' => false,
        )),
Nuncle answered 16/2, 2016 at 7:51 Comment(0)
M
1

The jquery file which is loaded in your header contains all UI element, and the one which automatically gets added in your file, have the children element which does not needed to upload, so you should remove it.

Myasthenia answered 17/2, 2016 at 5:39 Comment(0)
S
1

I had same problem and I did not use the jquery UI twice, my jquery UI was part of jquery DataTable.
My problem was solved with following code
Note: with this code we need to write code to close the UL when we do not click on UL

$(".gettingContactList").autocomplete({
     source:this.contactList,
     /*following focus function was written because when mouse
     was being hovered over the menu, the menu was disappearing*/
    focus:function(e,ui) { 
      $(e.toElement).parents().show()
    }
})
Sayette answered 5/7, 2017 at 9:31 Comment(0)
C
0

I had similar problem with typeahead

I used focus() and the problem was solved.

Example:

$(ele).typeahead({source: $scope.varMap['abc'], items: undefined});
$(ele).focus();
Cerecloth answered 3/7, 2019 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.