How to retain search terms in input field?

Viewed 36

I have an input element,

<div>
  <input type="text" id="query" autocomplete="off" />
</div>

that triggers an Apache Solr search. Upon submission the input field is cleared (possibly due to a page reload?).

How can I retain the query terms in the input field (or repopulate it, upon submission)?

1 Answers

I suspect the button associated with your page issues a submit (e.g. <button type="submit">Search</button>). If that is your case, you'll need to make sure you put the value the user submitted when you render the page again, you'll do this in the value attribute, e.g.

<div>
  <input type="text" id="query" autocomplete="off" value="the-value-the-user-entered" />
</div>

You could also issue an AJAX call when the user issues the search, in which case, the form won't reload and you won't need to worry about this, but then you will need to write the code to issue the AJAX call.

Related