My jQuery Breadcrumb
Breadcrumb = $(e.target).parentsUntil(".Menu").filter("li").children("a").map(function() {
return $(this).html().trim().replace(/Master Dashboard/g,"Dashboard");
}).get().reverse().join("<li><i class=\"fa fa-arrow-right\"></i></li>");
Returns
<i class="fa fa-user fa-2x"></i>
<span class="Title">Account</span>
<span class="Arrow"></span>
<li><i class="fa fa-arrow-right"></i></li>
<i class="fa fa-envelope fa-lg"></i>
<span class="Title">Messages</span>
<span class="Arrow"></span>
<li><i class="fa fa-arrow-right"></i></li>
<i class="fa fa-inbox fa-lg"></i>
<span class="Title">Inbox</span>
<span class="Counter">1,234</span>
Needs To Be
<li>
<i class="fa fa-user fa-2x"></i>
<span class="Title">Account</span>
</li>
<li><i class="fa fa-arrow-right"></i></li>
<li>
<i class="fa fa-envelope fa-lg"></i>
<span class="Title">Messages</span>
</li>
<li><i class="fa fa-arrow-right"></i></li>
<li>
<i class="fa fa-inbox fa-lg"></i>
<span class="Title">Inbox</span>
</li>
Question
I've tried several ways to .wrap("<li></li>") and to remove unwanted <span>...</span>'s from my breadcrumb however all have resulted in breaking my script.
How do I achieve my 'Needs To Be' by:
Remove all span.Arrow and span.Counter
Wrap each section in <li></li>
Live Demo
$(document).ready(function() {
"use strict";
$(document).on('click', function(e) {
var Breadcrumb;
if ($(e.target).closest($('.Menu')).length) {
Breadcrumb = $(e.target).parentsUntil(".Menu").filter("li").children("a").map(function() {
return $(this).html().trim().replace(/Master Dashboard/g,"Dashboard");
}).get().reverse().join("<li><i class=\"fa fa-arrow-right\"></i></li>");
console.log(Breadcrumb);
};
});
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="SideNav">
<!-- === Navigation Menu Starts === -->
<ul class="Menu">
<!-- === Minified Nav Starts === -->
<li class="MinifySideBar" style="display: none;">
<a href="javascript:;" data-title="Master-Dashboard">
<i class="fa fa-list-ul fa-2x"></i>
</a>
</li>
<!-- === Minified Nav Ends === -->
<!-- === Main Dashboard Starts === -->
<li>
<a href="javascript:;" data-title="Master-Dashboard">
<i class="fa fa-home fa-2x"></i>
<span class="Title">Master Dashboard</span>
</a>
</li>
<!-- === Main Dashboard Ends === -->
<!-- === User Account Starts === -->
<li>
<a href="javascript:;" data-title="Account">
<i class="fa fa-user fa-2x"></i>
<span class="Title">Account</span>
<span class="Arrow"></span>
</a>
<ul class="sub-menu First">
<!-- === User Account Dashboard Starts === -->
<li>
<a href="javascript:;" data-title="Account/Dashboard">
<i class="fa fa-home fa-lg"></i>
<span class="Title">Dashboard</span>
</a>
</li>
<!-- === User Account Dashboard Ends === -->
<!-- === User Messages Starts === -->
<li>
<a href="javascript:;" data-title="Account/Messages">
<i class="fa fa-envelope fa-lg"></i>
<span class="Title">Messages</span>
<span class="Arrow"></span>
</a>
<ul class="sub-menu">
<li>
<a href="javascript:;" data-title="Account/Messages/Compose">
<i class="fa fa-plus fa-lg"></i>
<span class="Title">Compose</span>
</a>
</li>
<li>
<a href="javascript:;" data-title="Account/Messages/Inbox">
<i class="fa fa-inbox fa-lg"></i>
<span class="Title">Inbox</span>
<span class="Counter">1,234</span>
</a>
</li>
<li>
<a href="javascript:;" data-title="Account/Messages/Drafts">
<i class="fa fa-pencil-square-o fa-lg"></i>
<span class="Title">Drafts</span>
<span class="Counter">123</span>
</a>
</li>
<li>
<a href="javascript:;" data-title="Account/Messages/Trash">
<i class="fa fa-trash fa-lg"></i>
<span class="Title">Trash</span>
<span class="Counter">12</span>
</a>
</li>
</ul>
</li>
<!-- === User Messages Ends === -->
</ul>
</li>
</ul>
</div>
UPDATE with document click
$(document).ready(function() {
"use strict";
$(document).on('click', function(e) {
var Breadcrumb;
if ($(e.target).closest($('.Menu')).length) {
Breadcrumb = $(e.target).parentsUntil(".Menu").find('li a').map(function() {
var el = $(this); // remove second span
$(el).find('span.Counter,span.Arrow,i').remove();
$(el).prepend("<i class=\"fa fa-arrow-right\"></i>");
var tx = $(el).html().replace(/Master Dashboard/g,"Dashboard").replace(/\t/g, "")
var out = "<li>" + tx + "</li>";
return out;
}).get().reverse();
console.log(Breadcrumb);
};
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="SideNav">
<!-- === Navigation Menu Starts === -->
<ul class="Menu">
<!-- === Minified Nav Starts === -->
<li class="MinifySideBar" style="display: none;">
<a href="javascript:;" data-title="Master-Dashboard">
<i class="fa fa-list-ul fa-2x"></i>
</a>
</li>
<!-- === Minified Nav Ends === -->
<!-- === Main Dashboard Starts === -->
<li>
<a href="javascript:;" data-title="Master-Dashboard">
<i class="fa fa-home fa-2x"></i>
<span class="Title">Master Dashboard</span>
</a>
</li>
<!-- === Main Dashboard Ends === -->
<!-- === User Account Starts === -->
<li>
<a href="javascript:;" data-title="Account">
<i class="fa fa-user fa-2x"></i>
<span class="Title">Account</span>
<span class="Arrow"></span>
</a>
<ul class="sub-menu First">
<!-- === User Account Dashboard Starts === -->
<li>
<a href="javascript:;" data-title="Account/Dashboard">
<i class="fa fa-home fa-lg"></i>
<span class="Title">Dashboard</span>
</a>
</li>
<!-- === User Account Dashboard Ends === -->
<!-- === User Messages Starts === -->
<li>
<a href="javascript:;" data-title="Account/Messages">
<i class="fa fa-envelope fa-lg"></i>
<span class="Title">Messages</span>
<span class="Arrow"></span>
</a>
<ul class="sub-menu">
<li>
<a href="javascript:;" data-title="Account/Messages/Compose">
<i class="fa fa-plus fa-lg"></i>
<span class="Title">Compose</span>
</a>
</li>
<li>
<a href="javascript:;" data-title="Account/Messages/Inbox">
<i class="fa fa-inbox fa-lg"></i>
<span class="Title">Inbox</span>
<span class="Counter">1,234</span>
</a>
</li>
<li>
<a href="javascript:;" data-title="Account/Messages/Drafts">
<i class="fa fa-pencil-square-o fa-lg"></i>
<span class="Title">Drafts</span>
<span class="Counter">123</span>
</a>
</li>
<li>
<a href="javascript:;" data-title="Account/Messages/Trash">
<i class="fa fa-trash fa-lg"></i>
<span class="Title">Trash</span>
<span class="Counter">12</span>
</a>
</li>
</ul>
</li>
<!-- === User Messages Ends === -->
</ul>
</li>
</ul>
</div>
this one outputs correct html for you:
see if it's ok now, cheers k
Related
I have the following html:
<ul class="collection">
<li>a
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
<li>s
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
<li>w
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
</ul>
I have an onclick listener on the UL (called taskList). console.log says the element UL (taskList) is an object. Can I iterate through the li's as follows:
for (const task in taskList) {
console.log(task);
}
No, for ... in will loop over the properties of the object. You could use for ... of to loop over the children.
const taskList = document.querySelector('ul.collection');
for (const task of taskList.children) {
console.log(task.outerHTML);
}
<ul class="collection">
<li>a
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
<li>s
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
<li>w
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
</ul>
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
This simple code not working:
$('li a').off('click').on('click', function(e){
var checkElement = $this.next();
checkElement.slideDown(500, function () {
checkElement.addClass('menu-open');
});
});
HTML:
<ul class="sidebar-menu">
<li>
<a href="unsafe:javascript:void(0)">
<i class="fa "></i><span class="ng-binding">Level 1</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li>
<a href="#/level2" class="ng-scope">
<i class="fa "></i> <span class="ng-binding">Level 2.1</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
</li>
<li>
<a href="#/level2" class="ng-scope">
<i class="fa "></i> <span class="ng-binding">Level 2.2</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
</li>
</ul>
</li>
</ul>
Debuging: checkElement in var checkElement = $this.next(); It contains an array of a position with the 'ul' element sibling:
print Chrome debugger
Error:
jquery-2.2.3.min.js: Uncaught TypeError: Cannot read property 'style' of undefined
The exception is thrown before executing the callback / complete function.
In jQuery the error is within the method: animate
I also have the Angular loaded.
Forgive me for bad English
$this.next(); should be $(this).next();
And prefer $('.sidebar-menu > li > a') to point to the direct <a> children
$('li > a').off('click').on('click', function(e){
var checkElement = jQuery(this).next('.treeview-menu');
checkElement.slideDown(500, function () {
checkElement.addClass('menu-open');
});
});
.treeview-menu {
display: none;
}
.menu-open {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="sidebar-menu">
<li>
<a href="unsafe:javascript:void(0)">
<i class="fa "></i><span class="ng-binding">Level 1</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li>
<a href="#/level2" class="ng-scope">
<i class="fa "></i> <span class="ng-binding">Level 2.1</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
</li>
<li>
<a href="#/level2" class="ng-scope">
<i class="fa "></i> <span class="ng-binding">Level 2.2</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
</li>
</ul>
</li>
</ul>
Finally solved.
It was not conflict with Angular, but with a loaded jQuery library (http://rocha.la/jQuery-slimScroll)
When running $.slimScroll thebody tag was overwritten for the slim effect in the scroll and this caused window.document.body to be null.
Removed library and everything working.
In my application a sidebar is there and it toggles when we click on it.Also I want to hide and show some table data but when I add code for this the sidebar doesnot toggle.May I know why this happened.
I want to hide table columns but when I did this my sidebar dropdown stops working.
This code is for sidebar toggle is working fine.
function goToUrl(id)
{
if(user_type_id!=3)
{
if(id==0)
{
$('.hideAndShow').toggle();
document.location="Doctor";//News
}
else
if(id==1)
{
$('.hideAndShow').toggle();
document.location="Device";//cases
}
if(id==2)
{
// $('#addJournals').toggle();
// $('#listingJournals').toggle();
$('.hideAndShow').toggle();
document.location="Associate";//journal
}
if(id==3)
{
$('.hideAndShow').toggle();
document.location="Patient";//gallery
}
}
else
{
if(id==0)
document.location="Doctor";
else
if(id==1)
document.location="Device";
if(id==2)
document.location="Associate";
else
if(id==3)
document.location="Patient";
}
}
<aside class="left-side sidebar-offcanvas" style="min-height: 260px;">
<!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar">
<!-- Sidebar user panel -->
<!-- <div class="user-panel">
<div class="pull-left image">
<img src="img/avatar3.png" class="img-circle" alt="User Image">
</div>
<div class="pull-left info">
<p id="imageAdmin"></p>
<i class="fa fa-circle text-success"></i> Online
</div>
</div> -->
<ul class="sidebar-menu">
<!-- <li id='dashboard'>
<a href="/dashboard">
<i class="fa fa-dashboard"></i> <span>Dashboard</span>
</a>
</li> -->
<li class='treeview' id='Doctor'>
<a href="#">
<!-- <i class="fa fa-table"></i> -->
<i id="icon-color" class="fa fa-stethoscope"></i>
<span onclick="goToUrl(0)">Manage Doctor</span>
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<li class="hideAndShow">
<a href="/addDoctor?id=0" style="margin-left: 10px;">
<i class="fa fa-angle-double-right"></i> Add
</a>
</li>
<li class="hideAndShow">
<a href="/doctor?id=1" style="margin-left: 10px;">
<i class="fa fa-angle-double-right"></i> Listing
</a>
</li>
</ul>
</li>
<li class='treeview' id='Device'>
<a href="#">
<!-- <i class="fa fa-table"></i> -->
<i id="icon-color" class="fa fa-mobile"></i>
<span onclick="goToUrl(1)">Manage Device</span>
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<li class="hideAndShow">
<a href="/addDevice" style="margin-left: 10px;">
<i class="fa fa-angle-double-right"></i> Add
</a>
</li>
<li class="hideAndShow">
<a href="/device?id=1" style="margin-left: 10px;">
<i class="fa fa-angle-double-right"></i> Listing
</a>
</li>
</ul>
</li>
<li class='active' id='Report'>
<a href="/reports">
<!-- <i class="fa fa-table"></i> -->
<i id="icon-color" class="fa fa-list-alt"></i>
<span>Reports</span>
</a>
</li>
</ul>
</section>
<!-- /.sidebar -->
</aside>
but when i add following code sidebar not toggle
$(document).ready(function(){
$("#hi").click(function(){
// $("p").hide();
$('td:nth-child(3)').hide();
});
$("#sh").click(function(){
// $("p").show();
$('td:nth-child(3)').show();
});
});
please help me.
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