Add class to children on click - html

<ul class="categories-menu">
<li class="cat-item">
A
<ul class="children">
<li class="cat-item">
<i class="fas fa-plus open"><-- This elment should expand children on click -->
A-CHILD
</li>
</ul>
</li>
</ul>
<ul class="categories-menu">
<li class="cat-item">
A
<ul class="children">
<li class="cat-item">
<i class="fas fa-plus open"> <-- This elment should expand children on click -->
A-CHILD
</li>
</ul>
</li>
</ul>
I try to add class open to submenu, when I click in specific .
Now all items with class submenu will open when I click on .
I want to open just closest submenu.
(function($){
$('.open').click(function(){
($(this).next().find('.children').length)
$('.children').addClass('opened');
});
})(jQuery);
EDIT:
var elems = document.getElementsByClassName("open");
Array.from(elems).forEach(v => v.addEventListener('click', function() {
this.parentElement.getElementsByClassName('children')[0].toggleClass('opened');
}));
How to add slideDown/slideUp.
I have console error: slideDown is undefined when I try add it.

Related

Dropdown button is not toggle in mobile/table view?

I have a dropdown menu in bootstrap where user clicked on to open sub-menu but when again user clicked on it is not closing. for reference check this image below mobile/table view
see when user clicked on caret is only opens not closing existing menu
Also: For reference you can check the live website:
HTML:
<li class="dropdown megamenu">
<li class="dropdown megamenu">
Kontakt & Anmeldung <span class="caret"></span>
<ul class="dropdown-menu">
<div class="container">
<div class="col-lg-2">
<ul>
<li>Kontakt</li>
<li>Reiseanmeldung</li>
<li>Videochat-Termin</li>
</ul>
</div>
</div>
</ul>
<div class="clearfix"></div>
</li>
<li class="dropdown megamenu">
About<span class="caret"></span>
<ul class="dropdown-menu">
<div class="container">
<div class="col-lg-2">
<ul>
<li>About us</li>
<li>Our Team</li>
</ul>
</div>
</div>
</ul>
<div class="clearfix"></div>
</li>
jQuery
$(document).ready(function () {
// Toggle div display
$(".dropdown .megamenu").click(function () {
$(this).find('.dropdown-menu').toggle();
});
});

Redirect to 'href ' and change active class of menu

I'm trying to change the active class of menu when I click on 'a' tag using jquery. I've searched a lot but none of them worked for me. when I click on a tag it redirects to 'href' attribute of the a tag and doesn't change the active menu.when I use 'e.preventDefult' it doesn't go to the 'href' but the active menu changes as I want.
What should I do?
This is the jquery I used:
<script>
$(document).ready(function(e) {
$('.navigation-menu-body ul li a').click(function (e) {
$('.navigation-menu-body ul li a.active').removeClass('active');
$(this).addClass('active');
$('.navigation-menu-body ul.navigation-active').removeClass('navigation-active');
$(this).parent().parent().addClass('navigation-active');
$('.navigation-icon-menu ul li.navigation-active').removeClass('active');
$('.navigation-icon-menu ul li').addClass('active');
window.location.href = $(this).attr('href');
console.log("href:", $(this).attr('href'));
e.preventDefault();
});
})
This is the html:
<div class="navigation">
<div class="navigation-icon-menu">
<ul>
<li data-toggle="tooltip" title="داشبورد">
<a href="#navigationDashboards" title="داشبوردها">
<i class="icon ti-pie-chart"></i>
</a>
</li>
<li data-toggle="tooltip" title="جشنواره های من">
<a href="#festivals" title="جشنواره های من">
<i class="icon ti-package"></i>
</a>
</li>
<li data-toggle="tooltip" title="کدهای جشنواره">
<a href="#serialNumbers" title="کدهای جشنواره">
<i class="icon ti-layers"></i>
</a>
</li>
<li data-toggle="tooltip" title="سوابق خرید">
<a href="#invoices" title="سوابق خرید">
<i class="icon ti-agenda"></i>
</a>
</li>
</ul>
</div>
<div class="navigation-menu-body">
<ul id="navigationDashboards" class="navigation-active">
<li class="navigation-divider">داشبورد</li>
<li>
<a class="active" href="/dashboard/index">فروش و مدیریت مشتری</a>
</li>
</ul>
<ul id="festivals">
<li class="navigation-divider">جشنواره ها</li>
<li>
جشنواره های من
</li>
</ul>
<ul id="serialNumbers">
<li class="navigation-divider">کدهای جشنواره</li>
<li>ثبت کد جدید </li>
<li>امتیازات من در هر اپلیکیشن </li>
</ul>
<ul id="invoices">
<li class="navigation-divider">سوابق خرید</li>
<li>خریدهای من </li>
</ul>
</div>
</div>
When clicking on an anchor tag, you goes to a different page, so in order for you to add an active class check the URL against the hrefs on document.ready and then add the class if the argument matches.
$document.ready(function() {
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,''));
$('.navigation-menu-body ul li a').each(function(){
if(urlRegExp.test(this.href)){
$(this).addClass('active');
}
});
});
I don't know much about jQuery but you can do it pretty easy in vanilla js.
First you specify an id for each li you got. then you need to check url so you can add active class for li tag of url you're currently in.for example:
<ul onload="checkUrl()">
<li id="tab1" data-toggle="tooltip" title="داشبورد">
<a href="#navigationDashboards" title="داشبوردها">
<i class="icon ti-pie-chart"></i>
</a>
</li>
<li id="tab2" data-toggle="tooltip" title="جشنواره های من">
<a href="#festivals" title="جشنواره های من">
<i class="icon ti-package"></i>
</a>
</li>
<li id="tab3" data-toggle="tooltip" title="کدهای جشنواره">
<a href="#serialNumbers" title="کدهای جشنواره">
<i class="icon ti-layers"></i>
</a>
</li>
<li id="tab4" data-toggle="tooltip" title="سوابق خرید">
<a id="tab4" href="#invoices" title="سوابق خرید">
<i class="icon ti-agenda"></i>
</a>
</li>
</ul>
then the js:
function checkUrl() {
if (document.location.href === "https://example.com/url-of-first-tab") {
document.getElementById("tab1").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-second-tab") {
document.getElementById("tab2").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-third-tab") {
document.getElementById("tab3").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-forth-tab") {
document.getElementById("tab4").classList.add("active");
}
}
Also keep it in mind that none of your a tags shoud have active class in html!

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

create a dropdown button in a selectable item in a list

I am using jQuery Ui to make list items selectable on my web app.
Inside each item from this list I have a dropdown button.
When I'm adding the selectable option to the list, the dropdown options doesn't open since the item (the button's parent) is being selected, and the button is not clickable anymore, as if the selectable option "hides" the clickability of the button.
How can i fix it?
My code looks like this:
<ul ui-selectable>
<li data-ng-repeat="item in items | filter:filterText">
<div type="button" class="dropdown " >
<a class="dropdown-toggle " id="dLabel" role="button" data-toggle= "dropdown" data-target="#" ng-href="/page.html">aaa
</a>
<ul class=" dropdown-menu " role="menu" >
<li > ng-href="#" >blabla1</a></li>
<li > ng-href="#" >blabla2</a></li>
<li > ng-href="#" >blabla3</a></li>
</ul>
</div>
</li>
First you code is missing a few parts. See comments below.
I took what you had and cleaned it up:
<ul ui-selectable>
<li data-ng-repeat="item in items | filter:filterText">
<div type="button" class="dropdown "> <a class="dropdown-toggle " id="dLabel" role="button" data-toggle="dropdown" data-target="#" ng-href="/page.html">aaa
</a>
<ul class=" dropdown-menu " role="menu">
<!-- You were missing the <a in front of each ng-href -->
<li> <a ng-href="#">blabla1</a>
</li>
<li> <a ng-href="#">blabla2</a>
</li>
<li> <a ng-href="#">blabla3</a>
</li>
</ul>
</div>
</li>
<!-- You were missing a closing ul -->
</ul>
and you can view it working here on js fiddle
I added the following to resources and it worked ok for me:
http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js
http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css

Bootstrap - Let the specified <li> tag open when data-toggle="collapse"

I wanted to let the "Data Management" li to be opened by default if it's child has a class "active".
Example in JSfiddle:
http://jsfiddle.net/MgcDU/5622/
HTML:
<div class="span4 well well-small">
<ul id="action-menu" class="nav nav-list">
<li class="nav-header">Navigation</li>
<li ><i class="icon-th"></i> Overview </li>
<li>
<i class="icon-book"></i> Data Management
<ul id="datamanagement-child" class="nav nav-list collapse">
<li class="active"><i class="icon-folder-close"></i> Company</li>
<li><i class="icon-user"></i> Employee</li>
<li><i class="icon-home"></i> Department</li>
<li><i class="icon-indent-right"></i> Position</li>
</ul>
</li>
<li>
<i class="icon-time" ></i> Time keeping
<ul id="timekeeping-child" class="nav nav-list collapse">
<li><i class="icon-edit"></i> Log employee</li>
<li><i class="icon-th-list"></i> Show logs</li>
</ul>
</li>
<li><i class="icon-briefcase"></i> Accounts</li>
<li><i class="icon-list-alt"></i> Reports</li>
<li class="divider"></li>
</ul>
</div>
How would I do this?
Solution 1
You can simply simulate a click() on data-management hash. This will make the toggle animation, too.
$("[href='#datamanagement-child']").click();
JSFIDDLE
Solution 2
Add in class for the ul after data-management.
<ul id="datamanagement-child" class="nav nav-list in collapse">
...
</ul>
JSFIDDLE
The result is like in the screen shot bellow:
If you only want it to be open if it has a child li with the class of "active", you can use this bit of jQuery that initiates the bootstrap collapse function if the active class is found:
if($("#datamanagement-child li.active").is("*")) {
$("#datamanagement-child").collapse("show");
}
OR if you just want it to show without the slide effect, you can use this code:
if($("#datamanagement-child li.active").is("*")) {
$("#datamanagement-child").addClass("in");
}
Any of the above code would need to go within your:
$(document).ready(function(){
// Code Here
});
I hope this helps.