Gravatar for

Question by Chris Ruderschmidt, Sep 1, 2015 7:37 PM

Is there a way to reference the search term?

Just looking for a way to reference the search term on the results page and place it elsewhere on the page.


<h2>you searched for <%=SEARCH_TERM%></h2>


1 Reply
Gravatar for

Answer by Jean-François L'Heureux, Sep 2, 2015 9:49 AM

The search terms are not available on the server-side when the Coveo JavaScript Search Framework is rendered in a Coveo for Sitecore search page. Also, the search terms could (and will likely) change during the search page lifetime as the user refines his query to find what he is looking for. Because of that, it is recommended to implement this feature in JavaScript on the client side.

  1. You could implement a custom component to do this. It could listen for state changes for the "q" state parameter and display it when it changes (see Implementing a Custom Component).
  2. You could create an HTML element with a unique id attribute value. Then, in JavaScript, you could add an event handler on the duringQuery, preprocessResults or querySuccess event to update your HTML element inner text with the "q" state parameter value.
Gravatar for

Comment by Chris Ruderschmidt, Sep 2, 2015 10:43 AM

I believe your answer is what I'm looking for, but just for completion sake, is this the jQuery line of code to reference it?

$("#search").coveo('state', 'q')


Gravatar for

Comment by Jean-François L'Heureux, Sep 2, 2015 10:57 AM

Yes, this line of code is the one to read the "q" state parameter of the Coveo JavaScript Search Framework jQuery plugin.

Ask a question