Setting active state for multi level menu items from Javascript function - javascript

I'm looking for some help with modifying an existing function which controls the highlighted states of a multi level js accordion menu. I have had to use javascript due to certain css elements not working in safari browsers.
My problem is due to the multi level aspect as when a sub link is clicked, the parent link above it then deselects. I need the parent link to stay active when its sub links are clicked and only deselects when a link outside of that list is clicked upon.
I understand the theory of adding a conditional statement but simply don't know how to apply it correctly within the function...any help would be very much appreciated.
Here is the existing function which tells a link to be active or selected:
var Lst;
function CngClass(obj){
if (Lst) Lst.className='.topnav';
obj.className='selected';
Lst=obj;
}
and here is the menu code:
<ul class="topnav">
<li>Home</li>
<li><a onclick="CngClass(this);" href="#">Top Link 2</a>
<ul>
<li><a onclick="CngClass(this);" href="#">Cookies</a></li>
<li><a onclick="CngClass(this);" href="#">Events</a></li>
<li><a onclick="CngClass(this);" href="#">Forms</a></li>
<li><a onclick="CngClass(this);" href="#">Games</a></li>
<li><a onclick="CngClass(this);" href="#">Images</a></li>
<li><a onclick="CngClass(this);" href="#">Navigations</a>
<ul>
<li><a onclick="CngClass(this);" href="#">CSS</a></li>
<li><a onclick="CngClass(this);" href="#">JavaScript</a></li>
<li><a onclick="CngClass(this);" href="#">JQuery</a></li>
</ul>
</li>
<li><a onclick="CngClass(this);" href="#">Tabs</a></li>
</ul>
</li>
<li><a onclick="CngClass(this);" href="#">Tutorials</a>
<ul>
<li><a onclick="CngClass(this);" href="#">HTML</a></li>
<li><a onclick="CngClass(this);" href="#">CSS</a></li>
<li><a onclick="CngClass(this);" href="#">JavaScript</a></li>
<li><a onclick="CngClass(this);" href="#">Java</a>
<ul>
<li><a onclick="CngClass(this);" href="#">JSP</a></li>
<li><a onclick="CngClass(this);" href="#">JSF</a></li>
<li><a onclick="CngClass(this);" href="#">JPA</a></li>
<li><a onclick="CngClass(this);" href="#">Contact</a></li>
</ul>
</li>
<li><a onclick="CngClass(this);" href="#">Tabs</a></li>
</ul>
</li>
<li><a onclick="CngClass(this);" href="#">Contact</a></li>
<li><a onclick="CngClass(this);" href="#">Upload script</a></li>
Many thanks for any help or ideas...

Oh dear, may I ask why you're not simply using JQuery? It simplifies your task so much more.
I've got a live example with JQuery (took me about 3 mins) you can try. I assume this is what you're trying to accomplish?
You can use the same method in normal javascript as well, except it's more work and probably less efficient.
The general idea is:
remove active class from the current li
find the 3rd parent from the clicked link (li, ul, li) and then apply the active class to
that one instead.
Here's the JQuery
$(document).ready(function(){
$(".topnav li a").click(function(){
//get the ul element
var checkElement = $(this).next();
if(checkElement.is('ul')) {
//check if it's visible
if(!checkElement.is(':visible')) {
$(this).parent().addClass('opened');
checkElement.slideDown();
}else{
$(this).parent().removeClass('opened');
checkElement.slideUp();
}
}
//get the tree node
var parentElement = $(this).parent().parent().parent();
if(parentElement.is('li')){
$(".topnav li.active").removeClass('active');
parentElement.addClass('active');
}
});
});
Here's an example where it highlights the li element you clicked.

Related

Navbar Elements Not showing

I have been on a project to build a website to help students with revision.
I decided to make use of a cool navbar I found online in the website as I am not much of a professional myself. However, the navbar elements (the navbar is a unordered list which has been styled) don't show when i put any sort of background picture in the 'top' div. When it doesn't have a background picture (like in the fiddle) it is impossible to use the navbar properly.
This is the navbar I used:
http://cssmenumaker.com/menu/responsive-flat-menu#
I have fiddled about the position on the cssmenu div, tried putting it in the nav id and classes (both have styles set in the CSS).
Here is the fiddle
https://jsfiddle.net/jgs5uqxr/
It says I need to attach some code with the fiddle so here is the formating of my navbar
<div id='cssmenu'>
<ul>
<li><a href='#'>Home</a></li>
<li class='active'><a href='#'>Products</a>
<ul>
<li><a href='#'>Product 1</a>
<ul>
<li><a href='#'>Sub Product</a></li>
<li><a href='#'>Sub Product</a></li>
</ul>
</li>
<li><a href='#'>Product 2</a>
<ul>
<li><a href='#'>Sub Product</a></li>
<li><a href='#'>Sub Product</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>About</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</div>
I would have expected my chnages to make a working multi-level navbar which I can work with so I can add links to the educational material for students.
Thanks in advance.

Children covering Parent element in JS

So I have a navigation bar with links and sub links. I currently have my links and sub links in the structure ul and li elements. The drop down navigation bar is designed using JavaScript, which is working as intended, minus one issue. I need the parent element to not disappear when the children element are shown. I have included a screenshot below along with the HTML and JS.
Children elements covering parent element
If someone provides an answer can you also explain why the parent gets covered up? I have tried to find this on stack and looked on the web and was not able to find exactly what I was looking for.
Thanks a bunch guys!
function main(){
$('.sub-elements').hide();
$('.main-elements').on('click',function(){
$(this).children().slideToggle(300);
});
}
$(document).ready(main);
<div class = "nav-bar">
<ul class = "nav-drop">
<li class = "main-elements">Link 1
<ul class = "sub-elements">
<li>Sub Link 1</li>
<li>Sub Link 2</li>
</ul>
</li>
<li class = "main-elements">Link 2
<ul class = "sub-elements">
<li>Sub Link 3</li>
<li>Sub Link 4</li>
<li>Sub Link 5</li>
<li>Sub Link 6</li>
</ul>
</li>
<li class = "main-elements"><a href='#'>Link 3</a>
<ul class = "sub-elements">
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
</ul>
</div>
sub links in the structure ul and li elements. The drop down navigation bar is designed using JavaScript, which is working as intended, minus one issue. I need the parent element to not disappear when the children element are shown. I have included a screenshot below along with the HTML and JS.
Children elements covering parent element
If someone provides an answer can you also explain why the parent gets covered up? I have tried to find this on stack and looked on the web and was not able to find exactly what I was looking for.
Thanks a bunch guys!
It should work:
function main() {
$('.sub-elements').hide();
$('.main-elements').on('click', function () {
$(this).find('.sub-elements').slideToggle(300);
});
}
$(document).ready(main);
Maybe you forget to add the children class in jQuery
function main(){
$('.sub-elements').hide();
$('.main-elements').on('click',function(){
$(this).children('.sub-elements').slideToggle(300);
});
}
$(document).ready(main);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class = "nav-bar">
<ul class = "nav-drop">
<li class = "main-elements">Link 1
<ul class = "sub-elements">
<li>Sub Link 1</li>
<li>Sub Link 2</li>
</ul>
</li>
<li class = "main-elements">Link 2
<ul class = "sub-elements">
<li>Sub Link 3</li>
<li>Sub Link 4</li>
<li>Sub Link 5</li>
<li>Sub Link 6</li>
</ul>
</li>
<li class = "main-elements"><a href='#'>Link 3</a>
<ul class = "sub-elements">
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
</ul>
</div>

Selecting list items that are not links

I have to convert user entered unordered lists in a content management system into a bootstrap menu (navbar) using jquery.
80% there apart from one challenge that I can't figure out a nice solution for - ie one that uses selectors rather than string manipulation or regex. After all, we all know that we never parse html with regex :)
So, using the limited UI tools at the user's disposal, they generate a list, usually a two level nested list something like this
<ul>
<li>Blah1
<ul>
<li><a href='http://xxxx'>Blah1a</a></li>
<li><a href='http://yyyy'>Blah1b</a></li>
<li>Blah1c</li>
<li><a href='http://zzzz'>Blah1d</a></li>
</ul>
</li>
<li><a href='http://aaaa'>Blah2</a></li>
<li>Blah3
<ul>
<li><a href='http://xxxx'>Blah2a</a></li>
<li><a href='http://yyyy'>Blah2b</a></li>
</ul>
</li>
</ul>
And so on... The important thing is that some of their list items are links, some are just text.
I need to select each block of text contained in an <li> that is not already wrapped in an <a> and wrap it in an <a href="#"> so that the above would be transformed into:
<ul>
<li><a href='#'>Blah1</a>
<ul>
<li><a href='http://xxxx'>Blah1a</a></li>
<li><a href='http://yyyy'>Blah1b</a></li>
<li><a href='#'>Blah1c</a></li>
<li><a href='http://zzzz'>Blah1d</a></li>
</ul>
</li>
<li><a href='http://aaaa'>Blah2</a></li>
<li><a href='#'>Blah3</a>
<ul>
<li><a href='http://xxxx'>Blah2a</a></li>
<li><a href='http://yyyy'>Blah2b</a></li>
</ul>
</li>
</ul>
Shouldn't be that difficult I'm sure, but after an hour of playing I'm getting nowhere.
Try
$('li').contents().filter(function(){
return this.nodeType == 3 && $.trim($(this).text()).length > 0
}).wrap('<a href="#" />')
Demo: Fiddle
One line of code here:
$("li").not(":has(>a)").wrapInner('<a href="#" />');
jsFiddle
So, if you want to just get the text nodes than, yeah, use .contents(), which will get all nodes, including text nodes. Here are 2 methods you can use below for this:
$("li").not(":has(>a)").contents().filter(function(){
return this.nodeType == 3 && $.trim($(this).text()).length > 0
}).wrap('<a href="#" />');
jsFiddle
And here is another approach, just for the sake of something different:
$("li").not(":has(>a)").each(function(){
var $contents = $(this).contents();
if ($contents.length)
$contents.eq(0).wrap('<a href="#" />');
});
jsFiddle

Very long (horizontaly) dropdown navigation menu jQuery issue

I'm trying to create a long dropdown nav bar. The HTML structure is the following:
<div id='cssmenu'>
<ul>
<li><a href='#'><span>Home</span></a></li>
<li><a href='#'><span>Products</span></a>
<ul>
<li><a href='#'><span>Product 1</span></a>
<ul>
<li><a href='#'><span>Sub Item</span></a></li>
<li><a href='#'><span>Sub Item</span></a></li>
</ul>
</li>
<li><a href='#'><span>Product 2</span></a>
<ul>
<li><a href='#'><span>Sub Item</span></a></li>
<li><a href='#'><span>Sub Item</span></a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'><span>About</span></a></li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
Visually it looks like this, but for now,
I'd like to add those red arrows for a horizontal scroll/slide the root <li> elements:
The demo of navbar with css is here: http://cssmenumaker.com/builder/800017
I have searched up the web but still could not find any solutions. If somebody has some ideas please share. Thank you!

Zurb Foundation dropdown menu in IE

I'm trying to get Foundation dropdown menus to work across browsers, but run into issues will ALL versions of IE (7-10, specifically).
I've implemented the JS dropdown menu that is described here (http://foundation.zurb.com/docs/components/dropdown.html) as a filter menu. It works in Chrome and FF but not in IE.
The console in IE gives me no indication of what's going on. My guess is it has something to do with IE caching the activeClass so that the dropdown menu never gets updated with new absolute coordinates? I can't tell, and I'm a bit lost.
Here's my code and a link to the live page (http://lp-beta.myshopify.com/collections/all -- password is 'stack') --
<div class='row options-style'>
<ul class='large-block-grid-3'>
<li><a href="#" data-dropdown='brand-filters'>BY BRAND</a></li>
<li><a href="#" data-dropdown='percent-filters'>BY COCAO PERCENTAGE</a></li>
<li><a href="#" data-dropdown='flavor-filters'>BY FLAVOR PROFILE</a></li>
</ul>
</div>
<ul class='f-dropdown options' id='brand-filters' data-dropdown-content>
<li><div class='option-value'><a href='/collections/all'>All brands</a></div></li>
{% for product_vendor in shop.vendors %}
<li><div class='option-value'>{{product_vendor | link_to_vendor }}</div></li>
{% endfor %}
</ul>
<ul class='f-dropdown options' id='flavor-filters' data-dropdown-content data-option-key='filter'>
<li><a class='option-value' id='chocolatey' data-option-value='.chocolatey'>Chocolatey</a></li>
<li><a class='option-value' id='citrus' data-option-value='.citrus'>Citrus</a></li>
<li><a class='option-value' id='fruity' data-option-value='.fruity'>Fruity</a></li>
<li><a class='option-value' id='floral' data-option-value='.floral'>Floral</a></li>
<li><a class='option-value' id='earthy' data-option-value='.earthy'>Earthy</a></li>
<li><a class='option-value' id='nutty' data-option-value='.nutty'>Nutty</a></li>
<li><a class='option-value' id='spicy' data-option-value='.spicy'>Spicy</a></li>
<li><a class='option-value' id='creamy' data-option-value='.creamy'>Creamy</a></li>
</ul>
<ul class='f-dropdown options' id='percent-filters' data-dropdown-content data-option-key='filter'>
<li><a class='option-value' id='forty' data-option-value='.forty'> Below 40%</a></li>
<li><a class='option-value' id='sixty' data-option-value='.sixty'> 41% to 60%</a></li>
<li><a class='option-value' id='eighty' data-option-value='.eighty'> 61% to 80%</a></li>
<li><a class='option-value' id='hundred' data-option-value='.hundred'> Above 80%</a></li>
</ul>
Foundation 4 does not support IE 8 and below.
Foundation 3 supports IE 8 but not IE 7. Check web site for Foundation 3 IE 7 patches.
Your page have multiple references to jquery.js (1.7, 1.9...) and zepto.js.
Zepto.js does not support IE; you can replace it with jquery.js; they are compatibile.
If possible, try to standardize on one jquery version.

Categories