posted on:January 7, 2008
Style Your Websites Search Field with JSCSS
Continuing to provide unobtrusive solutions, CSSG is happy to present SearchField. It serves as a way to style your search field and add behavior without any additional JavaScript or modifications in your markup. It features plug & play onfocus and onblur behaviors and auto suggestion like you’ve never seen before 🙂
Again, all you need is two lines of code, links to JavaScript and CSS files and that would be it.
Take a look at the Demo
| Download SearchField
Features
SearchField features 3 states for search input field based on user’s actions. We have inactive state, on click state (on focus to be exact) and inactive with user inputted text. You’ve seen these features on many sites but the difference here is that all you need to do is provide id to your search field, script takes it from there.
Additional feature, that can be turned on and off, is a search suggestion terms displayed as a dropdown box below the search field. You can navigate with up and down keys, or your mouse and you can edit your own keywords. Again, there is a difference between this and other suggestion or autocomplete scripts. With SearchField you don’t have to worry about additional markup or id’s for dropdown list, it is generated automatically.
Implementation
Download the script, extract the files and place the folder named "searchfield" in the root of your site. Add these two lines of code inside your head tag.
<link href="/searchfield/searchfield.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="/searchfield/searchfield.js"></script>
Now, all you need inside the markup is a search input field with exact id as named in .js file. Default id is "searchfield" but you can use anything.
Important thing is the parent element of the search field. I suggest using <p> tag as a parrent element. Here’s how original form looks like, so you can use something similar.
<form id="searchform" action="" method="post">
<p><input type="text" id="searchfield" name="searchfield" value="" />
<button type="submit">Go</button></p>
</form>
At the end modify css file to make it blend into your site.
Files
There are 2 main files, searchfield.js file and searchfield.css. Inside the .js file you can edit 4 parameters and get the results you want. The appearance of your field and dropdown suggestion box is modified via .css file. The class names are commented so you’ll find your way around it pretty well.
Parameters
Inside the searchfield.js we have 4 parameters: id, defaultText, suggestion, suggestionText.
First one represents id of the search field you want to add this script to. You can use your own id just make sure that it matches the search field in your html file. defaultText is a text that your search field will use as default value. When you set suggestion=true the script will generate search suggestions list for search field based on content of variable called suggestionText. If you don’t want suggestion list to appear set suggestion=false.
Preview and Download
Note: since Safari has it’s own behaviors for input elements this script is disabled for that browser.
Enjoyed the article?
Then don't miss our next one! Subscribe to our RSS feed or share it with your friends.
Share on FacebookTweet thisDeliciousStumbleUpon RedditSubscribe
Comments (59 Comments)
Sorry, the comment form is closed at this time.
dodot
January 9, 2008
SMASHINGAPPS.COM
January 9, 2008
Colin
January 9, 2008
cssglobe
January 9, 2008
kyle
January 10, 2008
kyle
January 10, 2008
cssglobe
January 10, 2008
cssglobe
January 10, 2008
Moz
January 16, 2008
hcabbos
January 18, 2008
ovi
January 19, 2008
ovi
January 19, 2008
Marc
January 26, 2008
Dragon
January 27, 2008
cssglobe
January 27, 2008
Vectorpedia (Rick)
January 28, 2008
meyabilisim
January 30, 2008
Thomas
February 21, 2008
cssglobe
February 22, 2008
ercan
February 27, 2008
Thomas
March 3, 2008
Thomas
March 3, 2008
Ledzep
March 14, 2008
cssglobe
March 14, 2008
Thomas
March 17, 2008
eric
March 25, 2008
Alan Hogan
April 9, 2008
ven
April 17, 2008
Richard
April 17, 2008
Gunter
May 1, 2008
gaby
May 7, 2008
Francisco
May 13, 2008
chester..
May 24, 2008
Orides Tomkiel
May 30, 2008
John
May 31, 2008
sezer
May 31, 2008
maggl
June 5, 2008
wwwcemil
June 7, 2008
MarK
June 10, 2008
Pete
June 12, 2008
asp-dersi
June 12, 2008
Kyrill
July 8, 2008
Phil
August 7, 2008
Phil
August 7, 2008
Mario
August 8, 2008
Steve A.
October 13, 2008
sibel kuzgun
December 16, 2008
kyko
December 22, 2008
Evan
December 30, 2008
Gabriel
January 28, 2009
Troy
March 8, 2009
Jérôme
March 11, 2009
anthony
March 31, 2009
Rafael R.P
April 28, 2009
Rafael R.P
April 28, 2009
Paul
May 5, 2009
Cynthia
May 19, 2009
beermohamed
May 25, 2009
david
June 15, 2009