Solved

Jquery ui autocomplete: Having trouble with clicking element in autosuggest list and populating input field.

Posted on 2014-10-11
2
328 Views
Last Modified: 2014-11-01
Hello, I'm having trouble with clicking a list element in autosuggest list and populating input field. It was working without the introduction to the top part (Highlighting) but i'm just not seeing what the issue is with that addition. Everything else is working perfectly.

(function( $ ) {
    // Custom autocomplete instance.
    $.widget( "app.autocomplete", $.ui.autocomplete, {

        // Which class get's applied to matched text in the menu items.
        options: {
            highlightClass: "ui-state-highlight"
        },

        _renderItem: function( ul, item ) {

            // Replace the matched text with a custom span. This
            // span uses the class found in the "highlightClass" option.
            var re = new RegExp( "(" + this.term + ")", "gi" ),
                cls = this.options.highlightClass,
                template = "<span class='" + cls + "'>$1</span>",
                label = item.label.replace( re, template ),
                $li = $( "<li/>" ).appendTo( ul );

            // Create and return the custom menu item content.
            $( "<a/>" ).attr( "href", "#" )
                       .html( label )
                       .appendTo( $li );

            return $li;

        }

    });

    // Create autocomplete instances.
    $(function() {

        $('.input-search').autocomplete({
            source: function(query, response) {
                $.ajax({
                    url: "app/func/func_search.cfc?method=search&returnformat=json",
                    dataType: "json",
                    data: {
                        searchPhrase: query.term
                    },
                    success: function(result) {
                    response(result);
                    },
                    select: function( event, ui ) {
                    log( ui.item ?
                    "Selected: " + ui.item.value + " aka " + ui.item.id :
                    "Nothing selected, input was " + this.value );
                    }
                });
            }
        });

      });

})( jQuery );

Open in new window

0
Comment
Question by:brihol44
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40376067
check line 17 : item.label.replace( re, template ),

replace label by value

so you've :
item.value.replace( re, template ),
0
 

Author Closing Comment

by:brihol44
ID: 40417757
Oh right! Thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now