Thanks a lot to @BotskoNet for helping me with the li height issue and @Dudu for the issue regarding the submenu not closing with other li . After fiddling around with @dudu 's code I was able to get the issue of the active class to also slideUp and slideDown the submenu.
Here's what I came with:
$("#topnav a").click(function(){
var el = $(this).parent();
if(el.hasClass('active') && $(this).next().is(':visible')){
var active = el.siblings('.active');
$(this).next().slideUp();
}
else if(el.hasClass('active') && !$(this).next().is(':visible')){
var active = el.siblings('.active');
$(this).next().slideDown();
}
else if(!el.hasClass('active')){
$(this).next().slideDown();
var active = el.siblings('.active');
active.children('ul:first').slideUp();
active.removeClass('active');
el.addClass('active');
}
});
And the live demo if you want to see it hands on (only first tab): http://codepen.io/PorototypeX/pen/swvtc