2

I am using jquery-ui autocomplete.

I want result list of the autocomplete search stayed open and closed when the area outside of the 'autocomplete text input area'&'search result list'.

I referenced keep ui autocomplete open at all times.

Now It keeps open. but can't find out how to close it when outside area is clicked.

http://jsfiddle.net/9r3avhoe/6/

$("input").autocomplete({
    source: ["Apple", "Boy", "Cat"],
    minLength: 0,
    response: function(event, ui) {
          //console.log(ui);
        },
        select: function(event, ui) {
          addObject(ui.item.value);
          return false;
        },
        focus: function(event, ui) {
          return false;
        },
        close: function(event, ui) {
          if (!$("ul.ui-autocomplete").is(":visible")) {
            $("ul.ui-autocomplete").show();
            $( "#search" ).focus();
          }

        }
});
Seungyeon Woo
  • 49
  • 2
  • 10
  • In my case , I also used Highcharts and It still doesn't work on chart area. About this problem I reffered this link ( https://stackoverflow.com/questions/52037475/focusout-on-an-element-conflicting-with-click-on-other-in-angular ) – Seungyeon Woo Oct 29 '18 at 08:51
  • Also use solution of Pawel Fus in https://github.com/highcharts/highcharts/issues/4606 – Seungyeon Woo Oct 29 '18 at 08:54

2 Answers2

3

You can use focusout and just trigger widget event.

$("input").focusout(function(event) {
    $("ul.ui-autocomplete").hide();
})
2

$("input").autocomplete({
    source: ["Apple", "Boy", "Cat"],
    minLength: 0,
  response: function(event, ui) {
     //console.log(ui);
    },
    select: function(event, ui) {
     addObject(ui.item.value);
     return false;
    },
    focus: function(event, ui) {
        console.log("focused");
     return true;
    },
    close: function(event, ui) {
     if (!$("ul.ui-autocomplete").is(":visible")) {
      $("ul.ui-autocomplete").show();
      $( "#search" ).focus();
     }

    }
});
$( "input").blur(function() {
   $("ul.ui-autocomplete").hide();
   
});
function addObject(id){
 var div = $('<p>'+id+'</p>').attr({"value":id}); 

 div.appendTo("#selected_obj");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<script
  src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
  integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
  crossorigin="anonymous"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/>
  <input type="text" />
<br/>
<br/>
<div id="selected_obj" style="border:solid 1px black;float:right">

</div>

Add a blur event then hide the popup. I hope this will help you.

Sumesh TG
  • 2,557
  • 2
  • 15
  • 29