I have made a customized select box using the followig html/css and jQuery.
div.selectBox {
position:relative;
display:inline-block;
cursor:default;
text-align:left;
line-height:30px;
clear:both;
color:#888;
}
span.selected {
width:167px;
text-indent:20px;
border:1px solid #ccc;
border-right:none;
border-top-left-radius:5px;
border-bottom-left-radius:5px;
background:#f6f6f6;
overflow:hidden;
}
span.selectArrow {
width:30px;
border:1px solid #60abf8;
border-top-right-radius:5px;
border-bottom-right-radius:5px;
text-align:center;
font-size:20px;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
background:#4096ee;
}
span.selectArrow,span.selected {
position:relative;
float:left;
height:30px;
z-index:1;
}
div.selectOptions {
position:absolute;
top:28px;
left:0;
width:198px;
border:1px solid #ccc;
border-bottom-right-radius:5px;
border-bottom-left-radius:5px;
overflow:hidden;
background:#f6f6f6;
padding-top:2px;
display:none;
}
span.selectOption {
display:block;
width:80%;
line-height:20px;
padding:5px 10%;
}
span.selectOption:hover {
color:#f6f6f6;
background:#4096ee;
}
and jQuery:
$('div.selectBox').each(function(){
$(this).children('span.selected').html($(this).children('div.selectOptions').children('span.selectOption:first').html());
$(this).attr('value',$(this).children('div.selectOptions').children('span.selectOption:first').attr('value'));
$(this).children('span.selected,span.selectArrow').click(function(){
if($(this).parent().children('div.selectOptions').css('display') == 'none'){
$(this).parent().children('div.selectOptions').css('display','block');
}
else
{
$(this).parent().children('div.selectOptions').css('display','none');
}
});
$(this).find('span.selectOption').click(function(){
$(this).parent().css('display','none');
$(this).closest('div.selectBox').attr('value',$(this).attr('value'));
$(this).parent().siblings('span.selected').html($(this).html());
});
});
But when we click arrow in the select box, the list appears. But it closes only when we click on the same arrow. How can I implement the list to close when I click any other part of the page?
HTML
<div class='selectBox'>
<span class='selected'></span>
<span class='selectArrow'>▼</span>
<div class="selectOptions" >
<span class="selectOption" value="Option 1">Option 1</span>
<span class="selectOption" value="Option 2">Option 2</span>
<span class="selectOption" value="Option 3">Option 3</span>
</div>
</div>
Use document click to hide the
.selectOptions:Working example: http://jsfiddle.net/FBuKG/