Instead of testing every click on an html dom element, you can bind the blur event to a specific menu item when you activate it, then turn it off when the blur event is fired. Replace these few lines:
//displaying the drop down menu $(this).parent().parent().find('.active').removeClass('active'); $(this).parent().addClass('active');
with these:
//displaying the drop down menu $('.active').removeClass('active'); $(this).parent().addClass('active'); $(this).blur(function() { $('.active').removeClass('active'); });
source share