1

I am implementing jQuery AutoComplete plugin like this:

HTML:

<input type='text' class='AutoComplete' url='/source/data'>

jQuery:

$( ".AutoComplete" ).autocomplete({
    source: "/source/data"
});

Above code is working. Now I want that source part of Autocomplete option should be fetched from input's url attribute (url='/source/data').

How is it possible ?

Thanks

1 Answer 1

2

You need to call the autocomplete() function (to set up the source), separately for each textbox.

function init(){
    $( ".autocomplete" ).each(function(ix,item){
       var $item = $(item),
           url = $item.attr('data-ac-url');
       $item.autocomplete({ source: url});
    });
}

$(document).ready(init);

This works with this HTML:

<input id='box1'
       class='autocomplete'
       value='' type='text'
       data-ac-url='/asp/autocomplete-src1.aspx' />
<input id='box2'
       class='autocomplete'
       value='' type='text'
       data-ac-url='/asp/ac-src2.php'/>

To specify the URL in the element markup, I recommend using an attribute like data-???, which complies with the HTML5 standard.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Thanks. Working perfectly. Can you please fix another thing. If there is not a data-ac-url property in element then it generates an error in firebug like this.source is not a function. Can we not fix it like that it do nothing if there is not a data-ac-url property.
Why not? You could probably insert an if statement in there to test for that. I wonder how you would do that? hmmmmm.....

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.