I made a very simple dropdown using a <div> (parent), <span> (current selection) and <ul> (options).
It works fine. What I now want to do is if the user clicks anywhere on the page, have it close, like a “real” <select> element.
What I do now is this:
$(document).delegate('body','click',function(){
if(isExpanded){close();}
});
And it works. What I am worried about is performance. Is it wise to listen for click events on the document node? Is there a better way?
Thank you.
You could also make use of the
blurevent. While your elements don’t support it by default, addingtabindex=...makes them fire theblurevent: http://jsfiddle.net/UgSTa/.HTML:
JavaScript: