Last active
December 18, 2021 05:48
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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