I recreated a Select box and its dropdown function using this:
$(".selectBox").click(function(e) {
if (!$("#dropDown").css("display") || $("#dropDown").css("display") == "none")
$("#dropDown").slideDown();
else
$("#dropDown").slideUp();
e.preventDefault();
});
The only problem is that if you click away from the box, the dropdown stays. I’d like it to mimic a regular dropdown and close when you click away, so I thought I could do a ‘body’ click:
$('body').click(function(){
if ($("#dropdown").css("display") || $("#dropdown").css("display") != "none")
$("#dropdown").slideUp();
});
But now, when you click the Select box, the dropdown slides down and right back up. Any ideas what I’m doing wrong? Thanks very much in advance…
You also need to stop a click from inside the
#dropdownfrom bubbling back up tobody, like this:We’re using
event.stopPropagation()it stops the click from bubbling up, causing the.slideUp(). Also your other 2 event handlers can be simplified with:visibleor.slideToggle(), like this overall: