Gruber
Gruber

Reputation: 2298

How to fire a search with instantsearch.js when clicking on a button

I'm using Algolia instantsearch.js with a very simple search box and a search button like this http://codepen.io/anon/pen/aNXLBV

I've changed the search box behavior with options.searchOnEnterKeyPressOnly, I'd like to have a search fired also when someone clicks on the Search button.

I'm sure this is a RTFM issue, but I couldn't find anything on this, neither in the Algolia general JS docs, also they look like two different beasts in some aspects.

So how can I have a search fired both on Enter or clicking on the Search button?

Upvotes: 1

Views: 2127

Answers (2)

user6375876
user6375876

Reputation:

Suppose you have a button of id search-button and search box of id search-box

If you are useing jquery do like so..

var search = instantsearch({
    appId: algoliaCredential.appId,
    apiKey: algoliaCredential.searcKey,
    indexName: 'INDEXNAME'
});

search.start();

$('#search-button').on('click', function(){
    var query = $('#search-box').val().trim();
    search.helper.setQuery(query).search(); //Set the query and search
})

I have the similar problem and i did that way. Hope this will work.

Upvotes: 8

vvo
vvo

Reputation: 2883

You can just bind your button click/submit and call:

// search = instantsearch(); ...
search.helper.search();

Indeed this is not something trivial/documented and makes it hard to know when you only know instantsearch.js.

We will try to make it easier.

Upvotes: 0

Related Questions