How to change the collapse menu icon with JS? - javascript

I have a main menu (collapse) that uses Bootstrap 3.3.7 and Font Awesome 5.0.1
What I am looking for :
When the menu is closed, a "plus" icon is displayed.
When the menu is open, a "minus" icon is displayed.
The "plus" icon is displayed on the menu but does not change.
I think there is a problem with my JS code.
<nav role="navigation" aria-labelledby="block-navigationprincipale-menu" id="block-navigationprincipale">
<ul class="menu nav navbar-nav">
<li class="expanded dropdown open">
<i class="fas fa-plus-circle fa-lg"></i> Boutiques
<ul class="menu dropdown-menu">
<li>
<i class="fas fa-shopping-bag fa-lg"></i> Boutiques
</li>
<li>
<i class="fas fa-gift fa-lg"></i> Produits
</li>
<li>
<i class="fas fa-sign-language fa-lg"></i> Services
</li>
</ul>
</li>
<li class="expanded dropdown">
<i class="fas fa-plus-circle fa-lg"></i> Groupes
<ul class="menu dropdown-menu">
<li>
<i class="fas fa-users fa-lg"></i> Groupes
</li>
<li>
<i class="fas fa-newspaper fa-lg"></i> Annonces
</li>
<li>
<i class="fas fa-file-alt fa-lg"></i> Articles
</li>
</ul>
</li>
<li>
<i class="fas fa-id-card fa-lg"></i> Profils
</li>
</ul>
</nav>
Here is my JS code. Something is wrong with it.
(function ($) {
$(".collapse-change-icon").on('shown.bs.collapse', function () {
$(this).find('[data-fa-i2svg]').removeClass("fa-plus-circle").addClass("fa-minus-circle");
});
$(".collapse-change-icon").on('hide.bs.collapse', function () {
$(this).find('[data-fa-i2svg]').removeClass("fa-minus-circle").addClass("fa-plus-circle");
});
})(window.jQuery);

You have not used the right bootstrap events for dropdown. Try this one.
jQuery(document).ready(function ($) {
$(".dropdown").on('shown.bs.dropdown', function () {
$(this).find('[data-fa-i2svg]').removeClass("fa-plus-circle").addClass("fa-minus-circle");
});
$(".dropdown").on('hidden.bs.dropdown', function () {
$(this).find('[data-fa-i2svg]').removeClass("fa-minus-circle").addClass("fa-plus-circle");
});
});
REf: https://getbootstrap.com/docs/3.3/javascript/#dropdowns-events

Related

Clicking on Admin Panel Side menu refreshes page instead of expanding the menu item

I am facing a weird issue with AdminLTE admin panel template which is I am using for my Angular 11 app. Its all loading the menu item. No issue with it. But when clicking an item refreshes the page instead of expanding the group.
Here is how it looks
When I clicking on Data Reconciliation for example, it reloads the page again. Then when I click it again, it expands the group
Here is the markup
<nav class="mt-2">
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
<!-- Add icons to the links using the .nav-icon class
with font-awesome or any other icon font library -->
<li class="nav-item menu-open">
<a href="#" class="nav-link active">
<i class="nav-icon fas fa-tachometer-alt"></i>
<p>
Dashboard
<i class="right fas fa-angle-left"></i>
</p>
</a>
<ul class="nav nav-treeview">
<li class="nav-item">
<a href="#" class="nav-link">
<i class="nav-icon fas fa-copy"></i>
<p>
Data Reconciliation
<i class="fas fa-angle-left right"></i>
<span class="badge badge-info right">6</span>
</p>
</a>
<ul class="nav nav-treeview">
<li class="nav-item">
<a href="pages/layout/top-nav.html" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>Top Navigation</p>
</a>
</li>
<li class="nav-item">
<a href="pages/layout/top-nav.html" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>Top Navigation</p>
</a>
</li>
</ul>
</li>
<li class="nav-header">Reports</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="nav-icon fas fa-copy"></i>
<p>
Layout Options
<i class="fas fa-angle-left right"></i>
<span class="badge badge-info right">6</span>
</p>
</a>
<ul class="nav nav-treeview">
<li class="nav-item">
<a href="pages/layout/top-nav.html" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>Top Navigation</p>
</a>
</li>
</ul>
</li>
<li class="nav-item">
<a href="./index3.html" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>Dashboard v3</p>
</a>
</li>
</ul>
</li>
</ul>
</nav>
What could be the reason? Is this issue have any connection with Angular?
Replace ALL
href="#"
by
[routerLink]=""
This should works.

Add active class to li and parents through jquery

I'm a beginner to javascript, I've been trying to add the active class to a li using jquery i.e. When its clicked, expand li and make it active:
<script>
$(document).ready(function() {
var url = window.location['pathname'];
jQuery('li a').each(function() {
if($(this).attr('href') == url)
$(this).parent().addClass('active');
});
});
</script>
This only makes the sub-menu active but not expanded. But when I manually added the active class (as shown below) to treeview and li, it expanded and became active. How to dynamically add the active class through jquery?
<aside class="main-sidebar">
<section class="sidebar">
<ul class="sidebar-menu" data-widget="tree">
<li class="header">MENU</li>
<li class="treeview active">
<a href="#">
<i class="fa fa-table"></i> <span>USER</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li><i class="fa fa-plus"></i> Add</li>
<li class="treeview active">
<a href="#"><i class="fa fa-circle-o"></i> Profile
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li class="active"><i class="fa fa-circle-o"></i> View </li>
<li><i class="fa fa-circle-o"></i> Settings </li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
</aside>
So, you need to apply active class on all .treeview using parents:
$(this).parents('.treeview').addClass('active');
Actually, changing parent() to parents() fixed it:
$(this).parents().addClass('active');

How do I keep my bootstrap submenu expanded on page load?

I am trying to make my sub menu item stay expanded and selected on page load using bootstrap, I am not sure how to do this in javascript. Here's my html:
JSfiddle
<ul class="ul-privacy-sidebar hidden-xs">
<li>
<a class="hidden-xs" href="#">
<span class="fa-stack">
<i class="fa fa-circle fa-stack-2x ps-redorange"></i>
<i class="mdi mdi-home fa-stack-1x fa-inverse"></i>
</span> Privacy & Security
</a>
</li>
<li>
<a href="http://10.60.51.91/monster-responsive/global/www/privacy-policy/privacy-policy.html">
<span class="fa-stack">
<i class="fa fa-circle fa-stack-2x ps-lt-green"></i>
<i class="mdi mdi-file-document fa-stack-1x fa-inverse"></i>
</span> Privacy Policy
</a>
</li>
<li>
<a href="#">
<span class="fa-stack">
<i class="fa fa-circle fa-stack-2x ps-teal"></i>
<i class="mdi mdi-bullhorn fa-stack-1x fa-inverse"></i>
</span> Cookies, Ads & More
</a>
</li>
<li>
<a href="#">
<span class="fa-stack">
<i class="fa fa-circle fa-stack-2x ps-orange"></i>
<i class="mdi mdi-settings fa-stack-1x fa-inverse"></i>
</span> Account Management
</a>
</li>
<li>
<a href="#">
<span class="fa-stack">
<i class="fa fa-circle fa-stack-2x ps-pink"></i>
<i class="mdi mdi-file fa-stack-1x fa-inverse"></i>
</span> Resume Security
</a>
</li>
<li>
<a href="#">
<span class="fa-stack">
<i class="fa fa-circle fa-stack-2x ps-redorange"></i>
<i class="mdi mdi-alert-outline fa-stack-1x fa-inverse"></i>
</span> Email Scams
</a>
</li>
<li class="active">
<a href="#faq-dropdow" class="list-group-item list-group-item-success" data-toggle="collapse" data-parent="#MainMenu">
<span class="fa-stack">
<i class="fa fa-circle fa-stack-2x ps-teal"></i>
<i class="mdi mdi-help fa-stack-1x fa-inverse"></i>
</span> FAQs <i class="mdi mdi-chevron-down"></i>
</a>
<div class="collapse open visible-desktop" id="faq-dropdow">
About Monster
Data Collection
Data Usage
Data Sharing
Data Storage
Cookies & Tracking
Advertising
Account Profile & Management
Email & Other Communications
Safeguarding Data
Job Application Process
Privacy Standards
</div>
</li>
<li>
<a href="#">
<span class="fa-stack">
<i class="fa fa-circle fa-stack-2x ps-lt-green"></i>
<i class="mdi mdi-email fa-stack-1x fa-inverse"></i>
</span> Contact Us
</a>
</li>
</ul>
using the default bootstrap example:
I only added class="open" to the parent li-item
HTML
Toggle navigation
Brand
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Link <span class="sr-only">(current)</span></li>
<li>Link</li>
<li class="dropdown open">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
<li role="separator" class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li>Link</li>
<li class="dropdown active">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li><a href="#"/></li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->

JQuery basics about child and parent elements

My code for a 2 level menu goes like this:
<ul class="nav nav-stacked navbar-fixed" id="side-menu">
<li class="active ">
<a href="index.html" data-toggle="tooltip" data-placement="bottom" title="Click here to go to home page">
<i class="fa fa-home fa-fw"></i> Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="">
<a href="#" data-toggle="tooltip" data-placement="bottom" title="xyz">
<i class="fa fa-users fa-fw"></i> Level 1
<span class="destruct" style="float:right">X</span>
</a>
</li>
<li class="nav-header">
<a href="#" data-toggle="collapse" data-target="#comMenu">
<i class="fa fa-cogs fa-fw"></i> Level 1
<i class="fa fa-caret-down fa-fw"></i>
<span class="destruct" style="float:right"> X </span>
</a>
<ul class="nav nav-second-level nav-stacked collapse " id="comMenu">
<li>
<a href="#">
<i class="fa fa-exchange fa-fw"></i> Level 2.1
</a>
</li>
<li>
<a href="#">
<i class="fa fa-download fa-fw"></i> Level 2.2
</a>
</li>
<li>
<a href="#">
<i class="fa fa-check-circle fa-fw"></i> Level 2.3
</a>
</li>
</ul>
</li>
<ul>
Now I want to select the span with id=destruct, which is at level 1 and on click remove entire html in the li (including the second level ul tag). You must have got it that I am trying to make a "X", upon clicking which the menu item and any sub-menu item deletes.
However, I need your help in selecting the correct elements. For only Level 1 menu, the following jQuery code works:
$(".destruct").click(function(e){
$(this).parent().remove();
});
What could be the correct selector for Level 2 menu? I tried a few options like nth children and all, but cant seem to get it right.
You have this: li > a > span, so if you start in the span, you should look at the parent of the parent, of what is best, search for the closest "li" (because yo can change your html structure inside li's without tuch your jquery code). With a change in the html (add the x in all levels) you can get what #Juan suggested:
<ul class="nav nav-stacked navbar-fixed" id="side-menu">
<li class="active ">
<a href="index.html" data-toggle="tooltip" data-placement="bottom" title="Click here to go to home page">
<i class="fa fa-home fa-fw"></i> Home
<span class="sr-only">(current)</span>
</a>
</li>
<li class="">
<a href="#" data-toggle="tooltip" data-placement="bottom" title="xyz">
<i class="fa fa-users fa-fw"></i> Level 1
<span class="destruct" style="float:right">X</span>
</a>
</li>
<li class="nav-header">
<a href="#" data-toggle="collapse" data-target="#comMenu">
<i class="fa fa-cogs fa-fw"></i> Level 1
<i class="fa fa-caret-down fa-fw"></i>
<span class="destruct" style="float:right"> X </span>
</a>
<ul class="nav nav-second-level nav-stacked collapse " id="comMenu">
<li>
<a href="#">
<i class="fa fa-exchange fa-fw"></i> Level 2.1
<span class="destruct" style="float:right">X</span>
</a>
</li>
<li>
<a href="#">
<i class="fa fa-download fa-fw"></i> Level 2.2
<span class="destruct" style="float:right">X</span>
</a>
</li>
<li>
<a href="#">
<i class="fa fa-check-circle fa-fw"></i> Level 2.3
<span class="destruct" style="float:right">X</span>
</a>
</li>
</ul>
</li>
<ul>
$(function () {
$(".destruct").click(function() {
$(this).parent().parent().remove();//first option
$(this).closest('li').remove();//best option
});
});
Working example: http://codepen.io/anon/pen/ZGaGby

Prevent Side Menu Collapse in Bootstrap

<div id="wrapper">
<div class="navbar-default sidebar" role="navigation">
<div class="sidebar-nav navbar-collapse collapse">
<ul class="nav" id="side-menu">
<li>
<a class="active" href="#"><i class="fa fa-bar-chart-o fa-fw"></i> Operation <span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<i class="fa fa-bar-chart-o fa-fw"> </i> Request<span class="fa arrow"></span>
<ul class="nav nav-third-level">
<li>
<i class="fa fa-dashboard fa-fw"></i> Browse
</li>
<li>
<i class="fa fa-dashboard fa-fw"></i> Add
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
Hello,
I have a bootstrap (3) side menu who works great but I can't understand why third level collapse (and with him his parent) on click.
I'd like to prevent his collapse and make it works like the second level.
Any suggestions?
Thanks.

Categories

Resources