1

I'm attempting to do jQuery autocomplete such that I can search for multiple words.

For example, if I have smart, very smart, and super smart in a list I'd like to be able to start typing smar and have all three options show up.

This code will work in the sense that if I start typing from the very beginning like over it will suggest over smart which is correct. But it wouldn't suggest it if I type just smart which is the desired output.

Any idea how I can adjust the code so that I could search and suggest say a substring within the list?

http://jsfiddle.net/UKgD6/390/

var acList = ['smart', 'over smart', 'smart land', 'under smart', 'very smart' ]; $('#ac').autocomplete({ source: function( request, response ) { var matches = $.map( acList, function(acItem) { if ( acItem.toUpperCase().indexOf(request.term.toUpperCase()) === 0 ) { return acItem; } }); response(matches); } }); 

1 Answer 1

2

Your issue can be fixed by changing the indexOf() check. Change === 0 to !== -1. This will return anything that matches, no matter what the index of the search string is within the actual string.

http://jsfiddle.net/UKgD6/391/

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.