Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

keep nav open when child item is active

$('.nav-item li a').each(function() {
    if ($(this).hasClass('active')) {
      	$(this).parents("ul").css('display', 'block');
    }
})

//ex.
//html
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
	<li class="nav-item nav-parent">
        <a href="#" class="nav-link nav-link-parent">
      		<i class="fa-solid fa-file-lines"></i>
            <p class="nav-title">Parent</p>
		</a>
        <ul class="nav nav-treeview">
            <li class="nav-item">
                <a href="#" class="nav-link">
                    <p>Child</p>
                </a>
            </li>
		</ul>
	</li>
</ul>

//js
$(document).ready(function() {
	$('.nav-treeview li a').each(function() {
        if ($(this).hasClass('active')) {
            //show children
            $(this).parents("ul").css('display', 'block');
            //open nav-parent
            $(this).closest(".nav-parent").addClass('menu-open');
            //add class active in nav-parent-link
            $(this).closest(".nav-parent").find(".nav-link-parent").addClass('active')
        }
   	})
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #nav #open #child #item #active
ADD COMMENT
Topic
Name
6+7 =