5

I have the jQuery UI Autocomplete setup to my liking and working perfectly, but there is one fatal flaw. In my autocomplete I use a custom display like this example. I have something very similar built but with on exception...

The only difference is that I have multiple autocomplete elements of the same class on that one page. Only the first element shows the extra data line, the rest only show the basic autocomplete.

I can get the desired result by just iterating all of those class elements and calling the autocomplete on them, but I was hoping there was a better way of calling it once and having it "just work".

Here's how I'm adding the extra line:

.data( 'autocomplete' )._renderItem = function( ul, item ) {
  return $( '<li></li>' )
  .data( 'item.autocomplete', item )
  .append( '<a>' + item.label + '<br/><small>' + item.desc + '<small></a>' )
  .appendTo( ul );
};

I should note that I'm not getting any console exceptions at all.

1
  • I am having the same issue, did you ever get an answer? Commented May 16, 2011 at 18:37

4 Answers 4

2

The only way I can make this work is by changing my code from:

addautocomplete($('.tagEntry'));

To:

$('.tagEntry').each(function() {
     addautocomplete(this);
});
Sign up to request clarification or add additional context in comments.

1 Comment

This isn't what I would've expected, either. Hopefully addautcomplete adds the same function to each element, to save resources. I only use jQuery for prototyping these days, but I wonder if this is a bug, or if the authors decided to only have autocomplete work on the first selected element, and not all selected elements, and if so, why.
2

You simply need to override the function via the object prototype, instead of on a single instance.

$.ui.autocomplete.prototype._renderItem = function( ul, item ) {
  return $( '<li></li>' )
  .data( 'item.autocomplete', item )
  .append( '<a>' + item.label + '<br/><small>' + item.desc + '<small></a>' )
  .appendTo( ul );
};

Overwrite the function before activating any autocompletes, but after the script has loaded.

5 Comments

Thanks Josiah - One issue though. I have multiple autocompletes on the same page that pull from different sources, only this one class requires a description.
@sshefer - Then you can do it the way you are. $('#selector-for-particular-autocomplete').data( 'autocomplete' )._renderItem. Remember that data is associated with an element. If you need to change some data associated with a particular element make sure your selector is unique.
That doesn't solve the problem. Only the first autocomplete with that class shows the extra data, not the rest of them. Am I doing something wrong?
@sshefer - Yes. Add a special class to the autocomplete that you want to behave differently, then use that class (or id) selector to overwrite the instance function _renderItem
I am currently doing that ($('input.myclass').data('autocomplete')._renderItem) but only the first input with that class gets the extra line rendered. I have 3 more on that page that get the default autocomplete with just the item.label. There are 3 identical inputs - only the first one loaded on the page gets the extra description line, the others do not. The only way I have found to do this was to iterate over each one when the page loads.
2

Alternatively:

$(..).autocomplete(..).each(function () {
   $.data(this, "autocomplete")._renderItem = function (ul, item) { ... } })

Comments

0

I had the same issue as sshefer and I got the best solution for it:

Change the

.autocomplete( "instance" )._renderItem = function( ul, item )
by

$( ".input_class" ).each(function(){
    $( this ).autocomplete( "instance" )._renderItem = function( ul, item )...
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.