Reputation: 1
I'm currently working on a web application that has a menu system with submenus. I'm trying to implement arrow key functionality where the right arrow key should open a submenu, and the active class should be assigned to the corresponding link. However, I'm encountering an issue where pressing the right arrow key is opening all submenus recursively and incorrectly assigning the active class to multiple links. I've provided the relevant code below:
$(document).ready(function() {// Other functionality code...
$(document).keydown(function(e) {const activeLink = $('.main-link.active');const submenu = activeLink.next('ul');
switch (e.which) {
case 37: // Left arrow
if (submenu.is(':visible')) {
submenu.slideUp();
}
break;
case 39: // Right arrow (not working as intended)
if (!submenu.is(':visible')) {
submenu.slideDown();
}
break;
case 38: // Up arrow. functionality
if (activeLink.parent('li').prev('li').length > 0) {
activeLink.removeClass('active');
activeLink.parent('li').prev('li').find('.main-link').addClass('active');
}
break;
case 40: // Down arrow functionality
if (activeLink.parent('li').next('li').length > 0) {
activeLink.removeClass('active');
activeLink.parent('li').next('li').find('.main-link').addClass('active');
}
break;
}
$('.sidebar-toggle').click(function () {
// Toggle menu-open class
$('body').toggleClass('menu-open');
// Toggle title
if ($('body').hasClass('menu-open')) {
$(this).prop('title', 'Collapse Menu');
} else {
$(this).prop('title', 'Expand Menu');
}
// Close all child menus
$('.treeview-menu').slideUp();
});
// Handle menu item clicks
$('.main-link').on('click', function (event) {
const clickedLink = $(this);
const isActive = clickedLink.hasClass('active');
// Remove active class from other items
$('.main-link.active').removeClass('active');
if (!isActive) {
// Toggle current item
clickedLink.addClass('active');
}
// SlideToggle and SlideUp for submenus
clickedLink.next('ul').slideToggle();
clickedLink.parent('li').siblings('li').find('ul').slideUp();
});
});
When I press the right arrow key, instead of just opening the submenu associated with the active link, it's opening all submenus recursively and assigning the active class to multiple links. I tried many. different solutions and even
activeSubMenu.find('.main-link:first').addClass('active');
does not seem to work. I'm not sure what's causing this behavior. Could anyone help me identify the issue and provide a solution to achieve the desired functionality? Any suggestions or insights would be greatly appreciated!
edit: Here is the HTML code associated with this. I forgot to add it in the post here:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Navigation Menu</title>
</head>
<body>
<ul class="treeview">
<li class="main-link">
<a href="javascript:">
<i class="fas fa-warehouse"></i>
<span>Warehousing</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu menu-open">
<li class="treeview">
<a href="javascript:" class="main-link">
<span>Setup</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu menu-open">
<li class="treeview">
<a href="/warehousing/setup/division/division-list">Division</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/branch-plant">Branch/Plant</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/warehouses-stores">Warehouses & Stores</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/inventory-types">Inventory Types</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/container-type">Container Type</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/location-type">Location Type</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/location-level">Location Level</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/operation-codes">Operation Codes</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/resources">Resources</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/company-calendar">Company Calendar</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/purchase-exception">Purchase Exception</a>
</li>
<li class="treeview">
<a href="/warehousing/setup/warehouse-location">Warehouse Location</a>
</li>
</ul>
</li>
</ul>
</li>
<!-- New Inventory Module -->
<li class="main-link">
<a href="javascript:">
<i class="fas fa-cubes"></i>
<span>Inventory</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu menu-open">
<li class="treeview">
<a href="/inventory/stock-levels">Stock Levels</a>
</li>
<li class="treeview">
<a href="/inventory/audit-trail">Audit Trail</a>
</li>
<li class="treeview">
<a href="/inventory/reports">Reports</a>
</li>
</ul>
</li>
</ul>
</body>
</html>
Upvotes: 0
Views: 40