In today’s tutorial we are going to use a nice jQuery plugin which will change the style of your select boxes by converting them to use divs and list items.
The reason it will convert the select box to a div and list items is because now it is easier to style in what ever way you want. With normal select boxes you can style the top selected option but you can’t easily style the dropdown options of the select box. When these are list items you can apply a CSS class to the selected item and change the style.
What Is DropKick.js
Creating custom dropdowns is usually a tedious process that requires a ton of extra setup time. Oftentimes lacking conveniences that native dropdowns have such as keyboard navigation. DropKick removes the tedium and lets you focus on making it look good.
DropKick will also degrade gracefully so if Javascript is turned off then it will just keep the original select box.
How To Use DropKick.js
To start using DropKick.js you first need to download the files you need from GitHub.
All you have to do in copy the files over to your project and include the DropKick CSS file and DropKick JS file.
1 2 3 4 5 6 |
<!-- Include DropKick.css --> <link rel="stylesheet" href="./css/dropkick.css"> <!-- Include DropKick.js --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> <script src="./js/jquery.dropkick-1.0.0.js"></script> |
Now all we have to do is create a new select box and assign DropKick to this new select box.
1 2 3 4 5 6 7 8 9 |
<select name="country" class="default" tabindex="2"> <option value="">Try Me!</option> <option value="AU">Australia</option> <option value="CA">Canada</option> <option value="DE">Germany</option> <option value="JP">Japan</option> <option value="GB">United Kingdom</option> <option value="US">United States</option> </select> |
Assign DropKick to this select box by using the following Javascript method.
1 2 3 |
$(document).ready(function(){ $('.default').dropkick(); }); |
View the demo to see what this will create.
Attach A On Change Event
In most cases when using select boxes you want something to happen when you select and option.
In jQuery this can easily be done by assigning the change event to the select box.
1 |
$('select').change(); |
To add a change event to the dropkick select boxes can be done by using the following code.
1 2 3 4 5 |
$('.change').dropkick({ change: function (value, label) { alert('You picked: ' + label + ':' + value); } }); |
Add Your Own Styles
To create your own theme you need to assign a theme to your DropKick by using the following code.
1 2 3 4 5 6 |
$('.custom_theme').dropkick({ theme: 'mytheme', change: function (value, label) { $(this).dropkick('theme', value); } }); |
Now copy the below CSS and use change the styles to however you want your new dropdowns to look like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/** * Your theme name here * * Say something about the theme * * Give yourself some credit here * */ /* Container */ .dk_mytheme .dk_container {} .dk_mytheme a {} /* Toggle */<div style='display:none;'><!--961f95143e84035578efc1e30761e315--><a href='http://manicsquirrel.com/etherion/st/12-month-loan-lenders-not-brokers' rel='nofollow'>12 month loan lenders not brokers</a><!--/961f95143e84035578efc1e30761e315--></div> .dk_mytheme .dk_toggle {} .dk_mytheme .dk_toggle:hover {} /* Focus State */ .dk_mytheme.dk_focus, .dk_mytheme:focus {} .dk_mytheme.dk_focus .dk_toggle {} /* Open State */ .dk_mytheme.dk_open {} .dk_mytheme.dk_open .dk_toggle {} /* Options Menu */ .dk_mytheme .dk_options {} .dk_mytheme .dk_options a {} .dk_mytheme .dk_options a:hover, .dk_mytheme .dk_option_current a {} /* Inner options */ .dk_mytheme .dk_options_inner {} /* Set a width property here */ .dk_mytheme .dk_options_inner, .dk_mytheme.dk_touch .dk_options {} |
Download
DropKick.js files are hosted on Github so they are free to download and you will be told of any updates when you follow the project, just click the link below to download the files.
To see how DropKick.js works view the demo page.