0

So, I have a search bar on my site:

<form action="" method="GET" class="form-inline justify-content-center">
    <input type="text" id="s_bar2" name="search" size = "75" class="form-control mr-sm-2" aria-label="Search">
    <input class="btn btn-outline-primary my-2 my-sm-0" type="submit" value="Search">
</form>

And after a search is executed, I maintain the query in the bar by pulling it from the URL using this script:

 <script type="text/javascript">
     var elem = document.getElementById("s_bar2");
     const urlParams = new URLSearchParams(window.location.search);
     elem.value = urlParams.get('search');
</script>

However, the previous query only appears in the search box for a few milliseconds before vanishing. I have pinpointed the issue to my script:

 $(document).ready(function() {
$("#s_bar2").autocomplete({
  source: "/policy_suggest/"
});

});

But I cannot figure out how to resolve it. I use the script for suggesting queries while the user is typing. Why does it cause my updated value to disappear? I have tried moving around this and my first script for loading the previous query value, but nothing is working.

Noah Omdal
  • 89
  • 12
  • That looks like a weird way to do the autocomplete.... Been awhile since I used it, but should be using source with a function that does the ajax call.... – epascarello Mar 07 '19 at 17:41
  • Possible duplicate of [Understanding and implementing jQuery autocomplete with AJAX source and appendTo](https://stackoverflow.com/questions/11729588/understanding-and-implementing-jquery-autocomplete-with-ajax-source-and-appendto) – Louys Patrice Bessette Mar 07 '19 at 18:03
  • What does jQuery load have to do with this? – Barmar Mar 07 '19 at 18:31

1 Answers1

0

I would get rid of most of your code as autocomplete has its own AJAX Function (https://jqueryui.com/autocomplete/) and use term instead of search in your server side code

  $(document).ready(function() {
    $("#s_bar2").autocomplete({
      source: "/policy_suggest/"
    });
  });
imvain2
  • 15,480
  • 1
  • 16
  • 21
  • I updated the code with this, and the autocomplete is still working (thanks, smarter approach) but I still have the previous query only showing up in the new page's search bar for a few milliseconds before disappearing. Any ideas? – Noah Omdal Mar 07 '19 at 22:19