3

I want to set Jquery UI plugin to search only from the beginning of the string. I find some simillar questions but no usable answer. I have this code:

$.ajax({ url: "{!$basePath}/mesta.xml", dataType: "xml", success: function( xmlResponse ) { var data = $( "city", xmlResponse ).map(function() { return { value: $( "name", this ).text(), id: $( "name", this ).text() }; }).get(); $( ".autocomplete" ).autocomplete({ source: data, minLength: 0 }); } }); 

Thank you.

4
  • Is this what you are looking for? stackoverflow.com/questions/2382497/… Commented Oct 16, 2011 at 14:46
  • 1
    Or this? stackoverflow.com/questions/5791379/… Commented Oct 16, 2011 at 14:47
  • I know these topics but I don't know ho to aplicate it to my code. Commented Oct 16, 2011 at 15:00
  • I dont know how to implement it. Because I read the data from an XML file. Commented Oct 16, 2011 at 16:19

1 Answer 1

4

Just apply what is in the link @Samich posted to your source option:

source: function(req, response) { var re = $.ui.autocomplete.escapeRegex(req.term); var matcher = new RegExp( "^" + re, "i" ); response($.grep( data, function(item){ return matcher.test(item.value); }) ); }, 

And here is a fiddle demo: http://jsfiddle.net/jensbits/PekQZ/

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.