Skip to content

Instantly share code, notes, and snippets.

@rhaglennydd
Last active July 12, 2019 17:40
Show Gist options
  • Save rhaglennydd/7d2c7d5186f5abe635cb263b518e3be2 to your computer and use it in GitHub Desktop.
Save rhaglennydd/7d2c7d5186f5abe635cb263b518e3be2 to your computer and use it in GitHub Desktop.
Bootstrap 4 Multi-Level Dropdown
/*
* Adapted from: https://stackoverflow.com/questions/44467377/bootstrap-4-multilevel-dropdown-inside-navigation
*/
'use strict';
( function( $ ) {
$( '.dropdown-menu a.dropdown-toggle' ).on( 'click', function() {
var $subMenu,
$this = $( this ),
$ancestors = $this.parents( '.dropdown-menu' ),
$parent = $this.parent( '.dropdown' );
if ( !$this.next().hasClass( 'show' ) ) {
$ancestors.first().find( '.show' ).removeClass( 'show' );
$parent.removeClass( 'show' );
}
$subMenu = $this.next( '.dropdown-menu' );
$subMenu.toggleClass( 'show' );
$parent.toggleClass( 'show' );
$this.parents( 'li.menu-item.dropdown.show' ).on( 'hidden.bs.dropdown', function() {
$( this ).removeClass( 'show' ).find( '.dropdown-menu .show' ).removeClass( 'show' );
} );
return false;
} );
} )( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment