$( document ).on( "ready", function() { $( ".menu a" ).each( function() { var ul = $( this.parentNode.parentNode ).find( "> ul" ); var collapsable = ( ul.length && !$( ul[ 0 ] ).hasClass( "simply-empty" ) ); var href = this.getAttribute( "href" ); if ( collapsable && !href ) { $( this.parentNode.parentNode ).addClass( "collapsable" ); $( this ).on( "click", function( evt ) { evt.preventDefault(); $( this.parentNode.parentNode ).toggleClass( "open" ); } ); } } ); } );