Keep parent menu toggle Jquery - html

Hello I need to keep parent menu open when a corresponding submenu has been selected.
I have the following code for the menu
var $menu = $('#menu'),
$menu_openers = $menu.children('ul').find('.opener');
// Openers.
$menu_openers.each(function() {
var $this = $(this);
$this.on('click', function(event) {
// Prevent default.
event.preventDefault();
// Toggle.
$menu_openers.not($this).removeClass('active');
$this.toggleClass('active');
// Trigger resize (sidebar lock).
$window.triggerHandler('resize.sidebar-lock');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav id="menu">
<ul>
<li>
<span class="opener">Submenu</span>
<ul>
<li>Lorem Dolor</li>
<li>Ipsum Adipiscing</li>
<li>Tempus Magna</li>
<li>Feugiat Veroeros</li>
</ul>
</li>
<li>
<span class="opener">Another Submenu</span>
<ul>
<li>Lorem Dolor</li>
<li>Ipsum Adipiscing</li>
<li>Tempus Magna</li>
<li>Feugiat Veroeros</li>
</ul>
</li>
</ul>
</nav>
How I could achieve to keep it open base on selection?
thank you!

$('.opener').on('click', function(event) {
$(this).next().toggleClass('active');
});
#menu ul li ul
{
display:none;
}
.active{
display:block !important;
}
.opener{
cursor: default;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<nav id="menu">
<ul>
<li>
<span class="opener">Submenu</span>
<ul class="submenu deactive">
<li>Lorem Dolor1</li>
<li>Ipsum Adipiscing</li>
<li>Tempus Magna</li>
<li>Feugiat Veroeros</li>
</ul>
</li>
<li>
<span class="opener">Another Submenu</span>
<ul class="submenu deactive">
<li>Lorem Dolor</li>
<li>Ipsum Adipiscing</li>
<li>Tempus Magna</li>
<li>Feugiat Veroeros</li>
</ul>
</li>
</ul>
</nav>

Related

jQuery ul li trees - slideToggle only li with ul child

I know there are a lot of similar solutions.. This one almost works :)
I have an unsorted list:
<ul id="Tree" class="sub-menu">
<li class="folder">First level
<ul class="sub-menu">
<li>File 1.1</li>
<li class="folder">Second level
<ul class="sub-menu">
<li>File 2.1</li>
<li>File 2.2</li>
<li class="folder">Tretji nivo
<ul class="sub-menu">
<li>File 3.1</li>
<li>File 3.2</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
And the jQuery:
$('.folder').click(function(event) {
event.stopPropagation();
$(this).find('>.sub-menu').slideToggle();
});
Click on .folder works as desired. The problem is that the slideToggle is triggered on all li elements. So if I click on a li > a element the file is downloaded but the ul is toggled also. I want to prevent toggle when I click on an element that has no direct ul child.
You could add the folder class to the level text instead of the whole li tag :
$('.folder').click(function(event) {
event.stopPropagation();
$(this).next('.sub-menu').slideToggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="Tree" class="sub-menu">
<li><span class="folder">First level</span>
<ul class="sub-menu">
<li>File 1.1</li>
<li><span class="folder">Second level</span>
<ul class="sub-menu">
<li>File 2.1</li>
<li>File 2.2</li>
<li><span class="folder">Tretji nivo</span>
<ul class="sub-menu">
<li>File 3.1</li>
<li>File 3.2</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

Nested nav toggle closing parent

I have a simple nav structure for a nav that has toggles to open and close sub-menus. However, when I click on a nested menu the toggle works, but also closes the parent sub-menu. How can I set it so that the parent doesn't close on the click of the nested menu?
jQuery(document).ready(function($) {
//Toggle sub-menus
$('#menu .has-children').on('click', function(event) {
event.preventDefault();
$(this).children('.sub-menu').slideToggle(500);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="menu">
<li>item 1</li>
<li class="has-children">item 2
<ul class="sub-menu">
<li>sub-item 1</li>
</ul>
</li>
<li class="has-children">item 3
<ul class="sub-menu">
<li>sub-item 1</li>
<li class="has-children">sub-item 2
<ul class="sub-menu">
<li>sub-sub-item 1</li>
</ul>
</li>
</ul>
</li>
</ul>
event.preventDefault(); doesn't stop the event from traveling up the tree, you need to use event.stopPropagation(); to stop the click from also triggering the click event listeners on parent elements.
(You could also add this to li inside sub menus without .has-children, to prevent them from closing parent menus, though that's unnecessary if those nav items link to other pages)
jQuery(document).ready(function($) {
//Toggle sub-menus
$('#menu .has-children').on('click', function(event) {
event.preventDefault();
event.stopPropagation();
$(this).children('.sub-menu').slideToggle(500);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="menu">
<li>item 1</li>
<li class="has-children">item 2
<ul class="sub-menu">
<li>sub-item 1</li>
</ul>
</li>
<li class="has-children">item 3
<ul class="sub-menu">
<li>sub-item 1</li>
<li class="has-children">sub-item 2
<ul class="sub-menu">
<li>sub-sub-item 1</li>
</ul>
</li>
</ul>
</li>
</ul>

jQuery toggle submenu a href is not working

I have a menu and the link or "a href click" is not working.
jQuery does not give any errors that I am aware of, and the menu opens as desired, but nothing seems to take the user to stackoverflow.com for the link in my code below. I tried .parent a, but maybe this is not correct.
I made this example based on the example below which is slightly different from the code I posted below.
jsfiddle
$(function () {
$(".submenu").hide();
$(".parent a").click(function (e) {
var elems = $(this).closest('li');
elems.siblings('li').find('ul').hide();
if (elems.find('.submenu').length) {
$(".submenu:first", elems).toggle();
}
return false;
});
});
.year_2014 .monthly-archive {
display: none;
}
.january .archive {
display: none;
}
.february .archive {
display: none;
}
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<div class="menu">
<ul>
<li class="parent"> Hello
<ul class="submenu">
<li> Hello
</li>
<li> Hello
</li>
<li class="parent"> Hello
<ul class="submenu">
<li> Hello
</li>
<li> Hello
</li>
</ul>
</li>
</ul>
</li>
<li class="parent"> Hello
<ul class="submenu">
<li> Hello
</li>
<li> Click
</li>
</ul>
</li>
</ul>
</div>
If I using $(".parent > a").click(function (e) can click a link and redirect to link
$(function () {
$(".submenu").hide();
$(".parent > a").click(function (e) {
var elems = $(this).closest('li');
elems.siblings('li').find('ul').hide();
if (elems.find('.submenu').length) {
$(".submenu:first", elems).toggle();
}
return false;
});
});
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<div class="menu">
<ul>
<li class="parent">Hello
<ul class="submenu">
<li> Hello
</li>
<li> Hello
</li>
<li class="parent"> Hello
<ul class="submenu">
<li> Hello
</li>
<li> Hello
</li>
</ul>
</li>
</ul>
</li>
<li class="parent"> Hello
<ul class="submenu">
<li> Hello
</li>
<li> Click
</li>
</ul>
</li>
</ul>
</div>

Dropdown menu inside of another drop down menu is not working

I'm having trouble getting a drop-down menu inside another dropdown menu to work. The first level dropdown works fine, but for the second level, when I click on the option for the dropdown menu (Learning Profiles) nothing happens... the menu fails to display.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-md-2">
<nav id="sideNavBar">
<ul class="nav nav-pills nav-stacked">
<li id="navBarTitle">MENU</li>
<li class="dropdown">
Children <span class="caret"></span>
<ul class="dropdown-menu">
<li>
Add Child
</li>
<li>
Archive/Delete Child
</li>
<li>
Children Details
</li>
<li class="dropdown">
Learning Profiles<span class="caret"></span>
<ul class="dropdown-menu">
<li>
Observations
</li>
<li>
Learning Outcomes
</li>
<li>
Photos
</li>
</ul>
</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
there is little change in the html
<div class="container">
<div class="row">
<div class="col-md-2">
<nav id="sideNavBar">
<ul class="nav nav-pills nav-stacked">
<li id="navBarTitle">MENU</li>
<li class="dropdown">
Children <span class="caret"></span>
<ul class="dropdown-menu">
<li>
Add Child
</li>
<li>
Archive/Delete Child
</li>
<li>
Children Details
</li>
<li class ="dropdown-submenu">
Learning Profiles<span class="caret"></span>
<ul class="dropdown-menu">
<li>
Observations
</li>
<li>
Learning Outcomes
</li>
<li>
Photos
</li>
</ul>
</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
and add the following css
.dropdown-submenu {
position: relative;
}
.dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-top: -1px;
}
also you need to add javascript
$(document).ready(function(){
$('.dropdown-submenu a.test').on("click", function(e){
$(this).next('ul').toggle();
e.stopPropagation();
e.preventDefault();
});
});
working plunker : https://plnkr.co/edit/jjrowMvX3FJ7OAsFpiaA?p=preview

CSS - Added indentation is not working for nested ul li

I'm newbie to CSS, I want to add some extra spacing to the ul -lis which have parent ul.optiongroup. But those ul.optiongroup can also contain more ul.optiongroups and lis. So. Here is the html look like [Pl.see my old query for reference if required] and the JsFiddle:
NESTED UL-LIs:
<div id="target" class="treeselector">
<div class="styledSelect">
Select
</div>
<ul class="optiongroup expand" rel="Home">
<li class="title">
<span> </span>Home
</li>
</ul>
<ul class="optiongroup expand" rel="Products">
<li class="title">
<span class="fa fa-minus-square-o"> </span>Products
</li>
<li rel="PCPROducts1" class="expand">
PCPROducts1
</li>
<li rel="PCPROducts5" class="expand">
PCPROducts5
</li>
<ul class="optiongroup expand" rel="PCPROducts6">
<li class="title">
<span class="fa fa-plus-square-o"> </span>PCPROducts6
</li>
<ul class="optiongroup" rel="6th Product">
<li class="title">
<span class="fa fa-plus-square-o"> </span>6th Product
</li>
<li rel="Digital">
Digital
</li>
<li rel="Analog">
Analog
</li>
</ul>
</ul>
</ul>
</div>
Tried to add CSS like this:
.optiongroup li{
margin-left:40px;
border: 1px red solid; /* Just to see what's happening there */
}
How can I achieve this?
What's the easiest way to design ul -li with minimum css here.
You're using a lot of UL tags when its not needed.
You could try doing something like this:
<ul>
<li>Lorem</li>
<li>Aliquam</li>
<li>
<ul>
<li>Lorem ipsum .</li>
<li>Aliquam tincidunt</li>
<li>Vestibulum</li>
</ul>
</li>
<li>Lorem ipsum</li>
<li>Aliquam tincidunt</li>
</ul>
Notice that second UL is inside the LI