Dynamically adding filters in autocomplete.js


I’m using autocomplete.js and I’m trying to dynamically change the filters based on some checkboxes that the user selects before performing a search.

I thought I could do something like this: save the user’s filters in a global variable whenever the user makes a change, and use the variable in the filters param like this:

  source: $.fn.autocomplete.sources.hits(index, {
        hitsPerPage: 10,
        attributesToRetrieve: [
        filters: userchecked

But you know, if wishes were horses, beggars would ride. This doesn’t work. So I’m wondering if there is a way to change the filters parameter dynamically in autocomplete.js?

I’d be most grateful for any tips.

All best,


How do you update userchecked? Do you change its value or its content?

userchecked = [];
// or 

The first one will change the reference, which then won’t be the same as the filters property of the configuration. If you use methods like push, it will update the content without changing the reference, so it will still be the same as filters. That could be why it doesn’t work for you.

Also you could create a custom data source. Check out the code of a datasource for inspiration.

That being said, have you considered using instantsearch.js? It is a modular library for building search UI.

Thanks Bobylito — that was it. Pushing filters to the array did the trick. I’m planning to look into instantsearch.js for more complex stuff, definitely.

Can you share your code , I am trying same but its not working for me. thanks in advance