Simple Search with jQuery

What this snippet below does: cleans up a search query and reloads a corresponding page to display the results. This is just for the front-end!

The HTML:

<form id="search">
 <input id="search_tag" type="search" required>
</form>

The JavaScript:

$( "#search" ).submit(function( event ) {
  event.preventDefault();
  $site = $( "#search_tag" ).val().trim().replace(/\s/g, "_"); // clean up for URL
  $site = 'http://keymedium.com/articles/'+$site;
  window.history.pushState('obj', 'Articles', $site);
  location.reload();
});

See it in action @ Key Medium

perm_identityAli Jaffar

schedule

visibility647 reads

labelMarked:

jquery   regex   code   javascript  

Did you enjoy this post?  


menu