user475464
user475464

Reputation: 1763

how to use source in autocomplete in jquery?

below is the html input

<input type="text" class="autocomplete" id="search_data" name="search_data" value=""  data-link="currency_autocomplete.php" >

this is the jquery i use for autocomplete

$(function() { 
        $(".autocomplete").autocomplete({
            source: 'currency_autocomplete.php',
            minLength: 2,
            select: function( event, ui ) {
                $(this).val(ui.item.value);
                return false;}
        })
});

the above code is working fine

but i need to update jquery source: something like below

$(function() { 
        $(".autocomplete").autocomplete({
            source: $(this).attr('data-link'),  *** here i need update
            minLength: 2,
            select: function( event, ui ) {
                $(this).val(ui.item.value);
                return false;}
        })
});

Upvotes: 0

Views: 95

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68400

this is not .autocomplete on that context and that's the reason why you can't get data-link attribute value. If there is only one element with .autocomplete class you could use this

source: $(".autocomplete").data('link')

Use this for the case you have more elements with class .autocomplete

$(".autocomplete").each(function() {
    var $this = $(this);
    $this.autocomplete({
        source: $this.data('link'),
        minLength: 2,
        select: function( event, ui ) {
            $(this).val(ui.item.value);
            return false;
        }
    });
});

Upvotes: 1

Related Questions