hey guys,
I’ve build my own kind of select box in html with css and jquery.
<div class="select">
<ul>
<li class="destination option small darr loc">One</li>
<li class="destination option small loc">Two</li>
<li class="destination option small loc">Three</li>
<li class="destination option small loc">Four</li>
</ul>
</div>
jQuery:
//select box
$('.select ul li.option').click(function(e) {
e.stopPropagation();
$(this).siblings().slideToggle(100).removeClass('darr');
$(this).addClass('darr');
});
$(document).click(function(e) {
$('.select ul li.option').each(function() {
if ( !$(this).is(":hidden") ) {
$(this).not('.darr').slideToggle(100);
}
});
});
So when clicking on the first Item the selectbox expands like a dropdown. When clicking one of the options the select box collapses again.
I also want the selectbox to collapse when clicking somewhere else on the page, therefore I’m listening to click events on the $(document). That works just fine.
Only little bug I have is, when having multiple of those selectboxes on the page and I expand onr of them and click on another selectbox the previous one doesn’t collapse.
See my example here: http://jsfiddle.net/UWSUk/
When you expand the second select box and then directly click on the one above it doesn’t collapse. Both stay open.
Any idea how to fix that?
You can fix this by:
The id attribute comparison check should work because IDs must be unique in the page.
You can see it working here.
Here’s the Javascript:
And here is the HTML: