Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created September 21, 2012 20:07
Show Gist options
  • Save claudiosanches/3763602 to your computer and use it in GitHub Desktop.
Save claudiosanches/3763602 to your computer and use it in GitHub Desktop.
jQuery Select form
(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="select-custom"><span class="select-custom-inner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0});
var selectBoxSpan = $(this).next();
var selectBoxSpanInner = selectBoxSpan.find(':first-child');
selectBoxSpan.css({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();
});
});
}
}
});
})(jQuery);
/*
Usage:
$(document).ready(function($) {
$('#main-wrapper select').customStyle();
});
CSS:
select {
background: none;
border: none;
display: block;
height: 23px;
width: 337px;
padding: 5px 10px;
}
.select-custom {
background: url('images/select.png') no-repeat;
display: block;
height: 23px;
width: 317px;
padding: 5px 10px;
}
.select-custom-inner {
display: block;
position: relative;
top: 2px;
width: 280px;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment