URL: http://www.adamcoulombe.info/lab/jquery/select-box/
NOTE: THIS HAS BEEN MOVED TO GITHUB: If you have any ideas or improvements for this script feel free to fork or contribute or discuss over there: https://github.com/adamcoulombe/jquery.customSelect
Plugin Download: http://www.adamcoulombe.info/lab/jquery/select-box/customSelect.jquery.js. ...............Demo: http://www.adamcoulombe.info/lab/jquery/select-box/ ...............This lightweight, unintrusive technique uses the native select box functionality of the web browser, and overlays a stylable SPAN element in order to acheive your desired look. Since it makes use of default browser functionality, it can be treated just like any ordinary HTML select box.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Style Select Boxes Using jQuery + CSS</title> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript"> (function($){ $.fn.extend({ customStyle : function(options) { if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){ return this.each(function() { var currentSelected = $(this).find(':selected'); $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')}); var selectBoxSpan = $(this).next(); var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right')); var selectBoxSpanInner = selectBoxSpan.find(':first-child'); selectBoxSpan.css({display:'inline-block'}); selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'}); var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom')); $(this).height(selectBoxHeight).change(function(){ //selectBoxSpanInner.text($(this).val()).parent().addClass('changed'); selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed'); // Thanks to Juarez Filho & PaddyMurphy }); }); } } }); })(jQuery); $(function(){ $('select').customStyle(); }); </script> <style type="text/css"> body { font-family:Arial, Helvetica, sans-serif } span.customStyleSelectBox { font-size:11px; background-color: #f5f0de; color:#7c7c7c; padding:5px 7px; border:1px solid #e7dab0; -moz-border-radius: 5px; -webkit-border-radius: 5px;border-radius: 5px 5px; } span.customStyleSelectBox.changed { background-color: #f0dea4; } .customStyleSelectBoxInner { background:url(canvas-list-nav-item-arrow-.gif) no-repeat center right; } </style> </head> <body> <select class="styled"> <option>one</option> <option>two</option> <option>something</option> <option>4</option> <option>5</option> </select> </body> </html>
Comments
Subscribe to comments
You need to login to post a comment.

Nice plugin, it doesn't work well if the option has a value attribute though.
bar
In this case "bar" becomes "foo" in onchange.
Oops, i didn't mark it as code, it's supposed to read:
bar<option value="foo">bar</option>handy plugin. If you want the select option to use the text instead of the value change line 24 in the above example to:
selectBoxSpanInner.text($('option:selected',this).text()).parent().addClass('changed');
This is probably a dumb question, and I apologise, but if I give span.customStyleSelectBox a width (e.g. width: 250px;), I have noticed two things:
Am I being dumb or is this a problem with the plugin?
Thanks!
This is probably a dumb question, and I apologise, but if I give span.customStyleSelectBox a width (e.g. width: 250px;), I have noticed two things:
Am I being dumb or is this a problem with the plugin?
Thanks!
Thanks guys....
I have fixed the plugin. As paddy murphy pointed out, there was a problem with the way it was handling the selected text... all should be good now :)
Just used this in a project - saved me some time. You da man!
I was having the same issue as ninjamaster just had to add
select.styled { position: relative; z-index: 99; }
Hi,
I am having a problem with IE in general. If I set a width on the select e.g 130px, the drop down list box stays the same width as the select box. In firefox and other browsers the drop down list box expands to fit the content. Is there a way to fix this for IE?
Hi,
I am having a problem with IE in general. If I set a width on the select e.g 130px, the drop down list box stays the same width as the select box. In firefox and other browsers the drop down list box expands to fit the content. Is there a way to fix this for IE?
I love this plugin and have been using it for months on my site. See it in action at the "search in..." portion of my search box: http://www.orientaloutpost.com/
However, I want the dropdown to open on hover/mouseover instead of only when it's clicked on. Any ideas?
Is there anyway to set the width rather than it calculating the width on :first child. I'm using it for single digits so the select box looks a bit odd
The Width is not calculated on the first child, it is calculated based on the width of the actual select box