Get element starting jQuery-Ui autocomplete widget?

I use auto complete for several text input elements, I want to get data from different functions depending on the element that triggered the event, since I pass the web service link in the data attribute of the text element. I do not want to write different cars 3 times per page.

HTML: -

<input class="autocomplete" name="firstname" link='getfirstname'/>
<input class="autocomplete" name="middlename" link='getmiddlename'/>
<input class="autocomplete" name="lastname"
link='getlastname'/>

JQuery: -

$('.autocomplete').autocomplete({
    source: function (request, response) {
        jQuery.get($(this).data('link'), {
                typedValue: request.term
            }, function (data) {
                response(data);
            }, 'json');
        },
        minLength: 1
})

Demo

I answered How to get an input element that launches jQuery autocomplete widget? but didn’t help.

+4
source share
1 answer

Instead of $ (this) .data you need to use $ (this.element) .data JSFiddle

$('.autocomplete').autocomplete({
    source:function(request, callback){
        console.log($(this.element).data("link"));
        jQuery.get($(this.element).data('link'), {
                typedValue: request.term
            }, function (data) {
                response(data);
            }, 'json');
    },
    select: function(event, ui) {
        console.log(event.target.name);
        console.log($(this).prop('name'));
        console.log($(event.target).data("link"));
    }
})
+2

All Articles