8

I've found here that to overwrite one of the autocomplete events. But can somebody please provide me with example how to do the same?

Community
  • 1
  • 1
LA_
  • 18,018
  • 53
  • 160
  • 288

5 Answers5

17

The appendTo option does indeed work as expected, and if you inspect at the DOM, the <ul> results element will be attached to the element. However, due to absolute positioning generated by jQueryUI, the list still appears directly under the <input>.

That said, you can override the internal _renderItem to directly append results to a completely different element, for example:

HTML

<input id="autocomplete"/>
<div class="test">Output goes here:<br/><ul></ul></div>

JavaScript

$('input').autocomplete({
    search: function(event, ui) {
        $('.test ul').empty();
    },
    source: ["something", "something-else"]
}).data('autocomplete')._renderItem = function(ul, item) {

    return $('<li/>')
   .data('item.autocomplete', item)
   .append(item.value)
   .appendTo($('.test ul'));
};

I have also created a demo to demonstrate this. Please note that the latest jQuery library has not had jQueryUI tested against it fully, so I am using the previous version which allows me to select to include jQueryUI directly with the jsFiddle options.

andyb
  • 42,062
  • 11
  • 113
  • 146
  • Thanks, looks like this is what I am looking for - will test it more ;) – LA_ Oct 30 '11 at 11:48
  • 2
    It seems that the SELECT handler that would usually fire when one selects an autocomplete option doesn't work with this setup. Is there a way to make it work? – Martin Jul 10 '12 at 12:24
  • 1
    Correct. Since the options have been pushed to a different element, none of the jQueryUI events will be fired. You would need to duplicate the events on your custom output element to mimic the jQueryUI autocomplete events and that just seems wrong. You could force the jQueryUI element to a new position which would retain the events which might work for you. – andyb Jul 10 '12 at 13:14
  • Awesome! It will be really useful for me as I am trying to use Geonames web service with jQuery Mobile. Thanks!!!!!! – Nicolaesse Nov 25 '12 at 19:35
6
<div class="test">Output goes here:<br/></div>

<script>
  $("input#autocomplete").autocomplete({
    source: ["something", "something-else"],
    appendTo: ".text",
    position: { my: "left top", at: "left bottom", of: ".test" }
// other options here
  });
</script>
samura
  • 4,265
  • 1
  • 16
  • 23
  • 1
    Hmm, it doesn't work for me somehow - `$( "#search" ).autocomplete({ appendTo: ".test", source: "/search", minLength: 2});` What can be wrong here? – LA_ Oct 29 '11 at 20:46
  • do you have a div with a `class="test"`? – samura Oct 29 '11 at 20:49
  • Surely. I've tried this here - http://jsfiddle.net/Gn2dX/, it doesn't work also ((. – LA_ Oct 29 '11 at 20:56
  • `autocomplete` is part of `jquery-ui`. Try to load that file after jquery. Your example: http://jsfiddle.net/Gn2dX/2/ jquery-ui: http://jqueryui.com/download – samura Oct 29 '11 at 21:23
  • jquery-ui surely works. And it is loaded after jquery ui in my code, so it doesn't help... – LA_ Oct 30 '11 at 08:06
  • but it wasn't in the jsfiddle you sent me. did you try my example? http://jsfiddle.net/Gn2dX/2/ – samura Oct 30 '11 at 10:10
  • Yes, I am testing your example and it doesn't work for me (tried both with FF and Chrome). – LA_ Oct 30 '11 at 11:00
  • Ok. That's strange. As soon as I write the letter 's' in the field it gives me the 2 words. – samura Oct 30 '11 at 11:02
  • But is the result attached to 'test' class field? See here what happens on my PC - http://habrastorage.org/storage1/ccc5cb70/c4860d67/3b37b2bb/3216bf35.png (I've added a few br's). – LA_ Oct 30 '11 at 11:06
  • Yes, it works now, but this is not what I am looking for - once user types something into search field, I want to display results in the separate field (like Google live search works now, but I don't need this dropdown with options - I need search results). – LA_ Oct 30 '11 at 11:45
  • I like this answer in conjunction with the _renderItem solution. Works great for me. Thanx – kieste May 17 '14 at 13:53
2

I needed more control over where to put the data, so this is how I went about it:

$("#input").autocomplete({
    minLength: 3,
    source: [
        "ActionScript",
        "AppleScript",
        "Asp"
    ],
    response: function(event, ui) {
        console.log(ui.content);
        // put the content somewhere
    },
    open: function(event, ui) {
        // close the widget
        $(this).autocomplete('close');
    }
});
hle
  • 21
  • 1
2

hle's answer worked awesome for me and gives you more flexibility! Here is my test code that was modified by his answer:

$("#autocomplete").autocomplete({
    minLength: 3,
    source: ["something", "something-else"],
    response: function(event, ui) 
    {
        console.log(ui.content);
        // put the content somewhere
    },
    open: function(event, ui) 
    {
        // close the widget
        $(this).autocomplete('close');
    }
});
Qtpounder
  • 23
  • 4
0

Although this question is pretty old but i got a pretty easy solution. No hack, nothing just in jQuery way:

Instead of autocomplete response function, just add response data in div on success

    $(document).ready(function () {
    $("#book-code-search").autocomplete({
      minLength: 2,
      delay: 500,
      source: function (request, response) {
       $.ajax( {
        url: "server side path that returns json data",
        data: { searchText: request.term, param2 : $("#type").val()},
        type: "POST",
        dataType: "json",
        success: function( data ) {
          $("#data-success").html(data.returnedData); //returnedData is json data return from server side response
          /* response($.map(data, function (item) {
               return {
              label: item.FullDesc,
              value: item.FullDesc                      
             }
          })) */
        }
      });
     }
    });
   });  
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id='data-success' style='color: green;'></div>
<input type='text' placeholder='Enter Book Code' id='book-code-search' />
<input type='hidden' id='type' value='book'>
Avnish alok
  • 1,926
  • 19
  • 25