2

I'm jQuery beginner trying to make jQuery UI Autocomplete get data from txt file. Txt format is simple:

user1
user2
user3
etc.

Code is:

jQuery.get('users.txt', function(usersGet) {
    $( "#userLogin" ).autocomplete( {
        source: usersGet
    });
});

But unfortunately that does not work. Can you please help? Thanks!

3
  • check it in some debugger e.g. firebug what errors you are encountering? Commented May 21, 2012 at 9:47
  • and check if usersGet is filled or not? Commented May 21, 2012 at 9:47
  • Hi Guys, firebug says: CONSOLE -> GET users.txt (Response): user1 user2 user3. Displayed in column. So, I'm fetching user list, but somehow autocomplete tooltip does not appear. Commented May 21, 2012 at 9:55

2 Answers 2

4

The accepted solution did not work for me. This is how I read a .txt for my jQuery autocomplete input field:

$.ajax({
    url: "foo/bar.txt",
    dataType: "text",
    success: function(data) {
        var autoCompleteData = data.split('\n');
        $("#input").autocomplete({
            source: function(request, response) {
                var results = $.ui.autocomplete.filter(autoCompleteData, request.term);
                response(results.slice(0, 10)); // Display the first 10 results
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

Comments

3

From the documentation:

Source
An Array of Strings:
[ "Choice1", "Choice2" ]

An Array of Objects with label and value properties:
[ { label: "Choice1", value: "value1" }, ... ]

You need to amend your text file to be in this format:

["user1","user2","user3"]

Then change your jQuery to this:

$( "#userLogin" ).autocomplete({
    source: 'users.txt'
});

4 Comments

It's OK now. I just poorly joined jQ js file!
Hi once again, autocomplete bubble is showing up, but when I type something in input, tooltip is showing ALL entrys from list, even that not mathing... for ex. I can type: "aaaazzzz" and still have autocomplete bubble (in list there is NO such string)... There is more than 1600 users/elements in users.txt - but problem occurs even when I reduced list to 5 entrys. My script is like Rory wrote.
@rw79 you're probably best starting a new question for that problem.

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.