Skip to content

Instantly share code, notes, and snippets.

@expertrec
Last active December 18, 2021 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expertrec/15da84af77a01eb7a5e4a28a5e31f608 to your computer and use it in GitHub Desktop.
Save expertrec/15da84af77a01eb7a5e4a28a5e31f608 to your computer and use it in GitHub Desktop.
Creating simple js search for your website. More info on https://blog.expertrec.com/create-javascript-search-box-for-your-website/
<!-- Insert the below snippet where you want your searchbox to appear -->
<form method="get" action="">
<input type="text" name="search" id="search" value="" />
<input type="submit" name="submit" value="Search" />
</form>
// Insert the below snippet in your javascript, either inside <script> tag or in separate javascript file.
var defaultText = "Search...";
var searchBox = document.getElementById("search"); //default text after load
searchBox.value = defaultText;
//on focus behaviour
searchBox.onfocus = function() {
if (this.value == defaultText) {
//clear text field
this.value = '';
}
}
//on blur behaviour
searchBox.onblur = function() {
if (this.value == "") {
//restore default text
this.value = defaultText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment