Gravatar for jpdery@coveo.com

Question by jpdery, May 16, 2014 2:44 PM

Prevent standalone search box to redirect if no keywords

I see no options. Tried below code (baseline is SearchBox.html from downloaded zip) does not prevent the redirection:

$(function () {
    Coveo.Rest.SearchEndpoint.configureSampleEndpoint();

    $('#searchBox2').keypress(function (e) {
        if (e.which == 13) {
//this is handled
            e.stopImmediatePropagation();
            return false;
        }
    });     
    $('#searchBox2.CoveoSearchBox').keypress(function (e) {
        if (e.which == 13) {
            e.stopImmediatePropagation();
            return false;
        }
    });


    $('#searchBox2').coveo('initSearchBox', 'Search.html');

    $('#searchBox2 .CoveoSearchBox').keypress(function (e) {
        if (e.which == 13) {
            e.stopImmediatePropagation();
            return false;
        }
    });

    $('#searchBox2 .CoveoSearchButton').click(function (e) {
//this is not handled
        e.stopImmediatePropagation();
        return false;
    });     
});
2 Replies
Gravatar for olamothe@coveo.com

Answer by olamothe, May 16, 2014 2:58 PM

First, make sure you bind your event before the init of your interface (so that your event pass before everything else the framework might add).

Then, you can call stopImmediatePropagation , (jQuery doc )

This should stop any other handler bound on that input.

stopPropagation only stop the event from bubbling up.

Gravatar for dan@getfishtank.ca

Answer by Dan Cruickshank, May 16, 2014 3:54 PM

Additionally - here is how I addressed this problem to handle clicks & keypresses. If the input is empty, we never execute the search.

Coveo.$("html").on(Coveo.Events.InitializationEvents.afterComponentsInitialization, "#search", function (e, data) {

        Coveo.$(this).coveo('patch', "QueryController.executeQuery", function () {
            Coveo.$.trim(
                Coveo.$(this.root).find('.CoveoQueryBox').val()
                ) != ''
            && this.__executeQuery();
        });
});
Gravatar for jeremygauthier@smarttech.com

Comment by jgauthier, Feb 16, 2016 6:51 PM

Dan's solution worked great! Thanks!

Ask a question