So I'm trying to adapt this Dropdown menu on Joomla the styles work great as expected so I'll post the javascript includes on the head of my website:
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript' src='js/dropdown.js'></script>
<script type='text/javascript'>
$(function() {
$('.menu').droppy();
});
</script>
<script type='text/javascript'>
$(function() {
$('.menu').droppy({speed: 100});
});
</script>
ok I don't know why its is not working I'll post the dropdown.js should I post the jQuery too? it's really big!
$.fn.droppy = function(options) {
options = $.extend({speed: 250}, options || {});
this.each(function() {
var root = this, zIndex = 1000;
function getSubnav(ele) {
if (ele.nodeName.toLowerCase() == 'li') {
var subnav = $('> ul', ele);
return subnav.length ? subnav[0] : null;
} else {
return ele;
}
}
function getActuator(ele) {
if (ele.nodeName.toLowerCase() == 'ul') {
return $(ele).parents('li')[0];
} else {
return ele;
}
}
function hide() {
var subnav = getSubnav(this);
if (!subnav) return;
$.data(subnav, 'cancelHide', false);
setTimeout(function() {
if (!$.data(subnav, 'cancelHide')) {
$(subnav).slideUp(options.speed);
}
}, 500);
}
function show() {
var subnav = getSubnav(this);
if (!subnav) return;
$.data(subnav, 'cancelHide', true);
$(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
if (this.nodeName.toLowerCase() == 'ul') {
var li = getActuator(this);
$(li).addClass('hover');
$('> a', li).addClass('hover');
}
}
$('ul, li', this).hover(show, hide);
$('li', this).hover(
function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
);
});
};
My question here is: Why is it not working! I know that this is really complex (I don't anything about JavaScript) but if you help me I'll post a tutorial and edited files that will help a lot of people!
By the way I've download jQuery from the original site so I don't think that this can be the problem!
Thanks in advance!
Here is the HTML generated from 2 levels til the UL:
<div id="topmenu">
<div class="moduletabledropdown">
<ul class="menu">
<li id="current" class="first level0 home active"><span>Home</span></li>
<li class="level0 parent faq"><span>FAQ</span><ul class="level1">
<li class="first last level1 item-01"><span>Item 01</span></li></ul></li>
<li class="level0 parent the-news"><span>The News</span><ul class="level1"><li class="first last level1 item-02"><span>Item 02</span></li></ul></li>
<li class="level0 web-links"><span>Web Links</span></li><li class="last level0 parent news-feeds"><span>News Feeds</span><ul class="level1"><li class="first last level1 item-03"><span>Item 03</span></li></ul></li></ul></div>
Here is the HTML for a page that does work pure HTML CSS:
<html>
<head>
<title>droppy - Nested drop down menus</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type='text/javascript' src='assets/jquery.js'></script>
<link rel="stylesheet" href="assets/project-page.css" type="text/css" />
<!-- per Project stuff -->
<script type='text/javascript' src='javascripts/jquery.droppy.js'></script>
<link rel="stylesheet" href="stylesheets/droppy.css" type="text/css" />
<!-- END per project stuff -->
</head>
<body>
<div id='container'>
<h1>
droppy
<span class='subtitle'> - Nested drop down menus</span>
</h1>
<div id='sidebar'>
<ul id='project-nav'>
<li><a href='#overview'>Overview</a></li>
<li><a href='#example'>Example</a></li>
<li><a href='#usage'>Usage</a></li>
<li><a href='#download'>Download</a></li>
<li><a href='#known-issues'>Known Issues</a></li>
</ul>
<ul id='ohoa-nav'>
<li><a href='http://onehackoranother.com/projects/'>Back to projects »</a></li>
<li><a href='http://onehackoranother.com/'>Back to onehackoranother.com »</a></li>
</ul>
<a href='http://thepixeltrap.com' id='pixel-trap' title='The Pixel Trap: New Directory for Web Professionals'>
<img src='http://onehackoranother.com/images/pixel-16.png' alt='' /> The Pixel Trap - A New Directory for Web Professionals
</a>
</div>
<div id='main'>
<h2 class='first' id='overview'>Overview</h2>
<p>Quick and dirty nested drop-down menu in the jQuery styleee. I needed a nav like
this for a recent project and a quick Googling turned up nothing that really suited,
so droppy was born. It hasn't been designed with flexibility in mind - if you like
what you see, great, integration should be a breeze - otherwise I'd look for something
more configurable elsewhere.</p>
<h2 id='example'>Example</h2>
<ul id='nav'>
<li><a href='#'>Top level 1</a></li>
<li><a href='#'>Top level 2</a>
<ul>
<li><a href='#'>Sub 2 - 1</a></li>
<li>
<a href='#'>Sub 2 - 2</a>
<ul>
<li>
<a href='#'>Sub 2 - 2 - 1</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 1 - 1</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 3</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 4</a></li>
</ul>
</li>
<li><a href='#'>Sub 2 - 2 - 2</a></li>
<li>
<a href='#'>Sub 2 - 2 - 3</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 3 - 1</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 3</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 4</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>Sub 2 - 3</a></li>
</ul>
</li>
</ul>
<script type='text/javascript'>
$(function() {
$('#nav').droppy();
});
</script>
<h2 id='usage'>Usage</h2>
<p>No mandatory configuration options or nothin' here, just use include the Javascript/CSS
resources and insert the following code in your document head, or any other
suitable place:</p>
<div class='caption'>Javascript:</div>
<pre><script type='text/javascript'>
$(function() {
$('#nav').droppy();
});
</script></pre>
Don't mind unclosed divs that is not the full code!
Can you post some of your drop down menu HTML?
Right now you are applying the droppy plugin to the selector '.menu', which looks for an element on your page that has the CSS class 'menu', is that what you intended? If you want to apply the dropdown to an element with id="menu" you would use $('#menu').
Update
I would recommend using superfish instead, the mechanics of drop down menus are really hard to get right to make the menus easy to use. Superfish does an excellent job of being very forgiving for the site visitors.
I can't find anything particularly wrong with the code, seems to work ok. The only change I had to do to get it to work was to initially set the submenus to hidden using CSS. Try this out, you'll need to add your CSS back into the HTML. I would suggest using Firefox, Firebug and Firequery. This will give you insight into what exactly is breaking if there are any JavaScript errors.
<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="javascripts/jquery.droppy.js"></script>
<style>
ul#nav li ul {
display: none;
}
</style>
</head>
<body>
<h2 id='example'>
Example</h2>
<ul id='nav'>
<li><a href='#'>Top level 1</a></li>
<li><a href='#'>Top level 2</a>
<ul>
<li><a href='#'>Sub 2 - 1</a></li>
<li><a href='#'>Sub 2 - 2</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 1</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 1 - 1</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 3</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 4</a></li>
</ul>
</li>
<li><a href='#'>Sub 2 - 2 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 3</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 3 - 1</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 3</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 4</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>Sub 2 - 3</a></li>
</ul>
</li>
</ul>
<script>
$(function() {
$('#nav').droppy();
});
</script>
</body>
</html>
Since your <script> block is in the <head> tag, it executes before the page body is loaded, when there is no .menu element
You need to move your <script> block to the end of the <body> tag, or wrap it in $(function() { ... });.
Related
I have the following HTML
<ul>
<li><a ...>item 1</a></li>
<li>item 2
<ul>
<li><a ...>item 2-a</a></li>
<li><a ...>item 2-b</a></li>
</ul>
</li>
<li><a ...>item 3</a></li>
<li>item 4
<ul>
<li><a ...>item 4-a</a></li>
<li><a ...>item 4-b</a></li>
</ul>
</li>
<li><a ...>item 5</a></li>
<li><a ...>item 6</a></li>
</ul>
I want to add a class to 'item 2' and 'item 4' with jquery using .has but can't find a way.
I want to clarify my question:
I have a nested list with an unknown amount of items where some items are not linked. I want to add a class to all unliked items.
If you want to add the class depending to the content of the li you could use :contains selector :
$( "li:contains('item 2')" ).addClass('X');
Or filter and check text() content :
$( "li" ).filter(function(){
return $(this).text()==='item 4';
}).addClass('X');
If you want to add class using the index's use .eq() like Robiseb's answer show.
EDIT :
I don't know how many <li> without a link will be in the list. And I don't know what the text inside the <li> will be.
You could use :
$("li:not(:has(>a))").addClass('X');
Hope this helps.
$("li:not(:has(>a))").addClass('X');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li><a ...>item 1</a></li>
<li>item 2
<ul>
<li><a ...>item 2-a</a></li>
<li><a ...>item 2-b</a></li>
</ul>
</li>
<li><a ...>item 3</a></li>
<li>item 4
<ul>
<li><a ...>item 4-a</a></li>
<li><a ...>item 4-b</a></li>
</ul>
</li>
<li><a ...>item 5</a></li>
<li><a ...>item 6</a></li>
</ul>
Only selecting for list items without anchors, as clarified in the questions comments.
var parentList = $('ul'); // something more specific is assumed.
parentList.children('li').children().not('a').parent().addClass('some-class');
Using "contains" makes the solution dependent on content, not structure. Using "eq" or similar "nth"-like functionality is assuming that the given example is the only structure, whereas I think it is planned as generated code that may extend to a much longer length, but will always keep the structure defined in the snippet.
Try using .eq() documentation
$('li').eq(1).addClass('yourClass');
Be careful, eq starts to 0
Based on comments you want to check if it has a sub <ul>
$('li').has('ul').addClass('has-children');
// OR
$('li:has(ul)').addClass('has-children');
You can select item by your content, using :contains() selector
For more details: https://api.jquery.com/contains-selector/
Example: http://jsbin.com/nutekaxora/edit?html,css,js,output
$("li:contains('Item 1')").addClass('myClass');
Probably not the best way to do this, but use the "eq" method for this.
var first = $("ul").first();
$(first).find("li").eq(1).addClass("newClass"); /// This selects the second list item
$(first).find("li").eq(5).addClass("newClass"); // This selects the sixth list item
Here's a pen for this to show it in action.
http://codepen.io/raghavkanwal/pen/XjLjJM
Do you mean to detect all 'li' with 'ul' as a children?
https://jsfiddle.net/Ln3kt6ya/
May this example help you?
$('li').has('ul').addClass('error');
<style>
ul {color: #777777;}
ul.items-list {color: #880000;}
.your-class-name {color: #008800;}
</style>
<body>
<ul class="items-list">
<li><a>item 1</a></li>
<li>item 2
<ul>
<li><a>item 2-a</a></li>
<li><a>item 2-b</a></li>
</ul>
</li>
<li><a>item 3</a></li>
<li>item 4
<ul>
<li><a>item 4-a</a></li>
<li><a>item 4-b</a></li>
</ul>
</li>
<li><a>item 5</a></li>
<li><a>item 6</a></li>
</ul>
</body>
<script>
var $list = $('ul.items-list');
$list.children('li:nth-child(2), li:nth-child(4)').addClass('your-class-name');
</script>
I created a List in a Dropdown panel from Foundation. In that List I have links. This is my List:
<button class="button plzddmenu" type="button" data-toggle="PLZdropdown">Postleitszahl Suche</button>
<div class="dropdown-pane" id="PLZdropdown" data-dropdown data-auto-focus="true">
<ul class="plzlist">
<li><a class="plzclick">10000 - 19999</a></li>
<li><a class="plzclick">20000 - 39999</a></li>
<li><a class="plzclick">40000 - 59999</a></li>
<li><a class="plzclick">60000 - 79999</a></li>
<li><a class="plzclick">80000 - 99999</a></li>
</ul>
</div>
Now I'm using a jQuery script to detect, if the elements were clicked, and when they're clicked, to write them into the Button. This is my script:
<script type="text/javascript">
$(document).ready(function(){
$('.plzclick').on("click", function(){
$('.plzddmenu').html(this);
});
</script>
It all works fine, besides that my Elements disappear from my List, once I clicked them. Anyone know, why they dissapear and how I can fix this to make them stay?
You are moving your items to the button, You need just to change your button html to the clicked a tag html content.
$(this).html();// retruns the html content of the elemtent.
$(document).ready(function() {
$('.plzclick').on("click", function() {
$('.plzddmenu').html($(this).html());
console.log(this);
console.log($(this));
console.log($(this).html());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<button class="button plzddmenu" type="button" data-toggle="PLZdropdown">Postleitszahl Suche</button>
<div class="dropdown-pane" id="PLZdropdown" data-dropdown data-auto-focus="true">
<ul class="plzlist">
<li><a class="plzclick">10000 - 19999</a>
</li>
<li><a class="plzclick">20000 - 39999</a>
</li>
<li><a class="plzclick">40000 - 59999</a>
</li>
<li><a class="plzclick">60000 - 79999</a>
</li>
<li><a class="plzclick">80000 - 99999</a>
</li>
</ul>
</div>
<script type="text/javascript">
</script>
Trying to make javascript horizontal menu, but can't get second button to open its own items, (when i click the second button it opens the items that are for the first button) here is current code:
JavaScript:
$(document).ready(function() {
$(".menu-button,.menu-button1").click(function() {
$(".menu-bar").toggleClass("open");
});
})
HTML:
<ul class="menu">
<li title="home">menu</li>
<li title="pencil">pencil</li>
<li title="about">about</li>
</ul>
<ul class="menu-bar">
<li>Menu0</li>
<li>Home2000</li>
<li>About</li>
<li>Parent</li>
<li>About</li>
</ul>
<ul class="menu-bar">
<li>Menu1</li>
<li>About</li>
</ul>
Before I start my answer, let me explain jQuery a bit.
$(".menu-button,.menu-button1").click(function() {
$(".menu-bar").toggleClass("open");
});
This broken down:
$(".menu-button,.menu-button1").click(function() { -> When any item with class menu-button OR class menu-button1 is clicked
$(".menu-bar").toggleClass("open"); ->Toggle the "open" class for all elements in your page with class menu-bar.
Since you call all the menus instead of the specific one you want, it opens both of them.
So, be more specific by - for starters - using IDs, or unique/identifying classes:
JS:
$(document).ready(function() {
$(".menu-button.home").click(function() {
$(".menu-bar.home").toggleClass("open");
});
$(".menu-button.pencil").click(function() {
$(".menu-bar.pencil").toggleClass("open");
});
})
HTML:
<ul class="menu">
<li title="home">menu</li>
<li title="pencil">pencil</li>
<li title="about">about</li>
</ul>
<ul class="menu-bar home">
<li>Menu0</li>
<li>Home2000</li>
<li>About</li>
<li>Parent</li>
<li>About</li>
</ul>
<ul class="menu-bar pencil">
<li>Menu1</li>
<li>About</li>
</ul>
I agree with M.Doye's comment about using .each (but sorry, I can't answer directly).
I want to add that, it will be much easier with that kind of HTML structure I think:
<ul class="menu">
<li title="home">
Show Menu 1
<ul class="menu-bar">
<li>Menu1
</li>
</ul>
</li>
<li title="pencil">
Show Menu 2
<ul class="menu-bar">
<li>Menu2
</li>
</ul>
</li>
</ul>
The, click on the link and use .next() or .siblings or closest... to show the right ul.
But of course you'll have to rewrite you CSS :)
Here is updated code
$(document).ready(function () {
$(".menu-button,.menu-button1").click(function () {
$(this).siblings(".menu-bar").toggleClass("open");
})
})
<ul class="menu">
<li title="home">menu
<ul class="menu-bar">
<li>Menu1</li>
<li>About</li>
</ul>
</li>
<li title="pencil">pencil
<ul class="menu-bar">
<li>Menu0</li>
<li>Home2000</li>
<li>About</li>
<li>Parent</li>
<li>About</li>
</ul>
</li>
</ul>
here is a jsfiddle
Ok I am trying to create an accordance style menu with sub-menu in it but when I click the hamburger button, my secondary sub-menu also shows up by default. How to hide "list 2" when first click on the hamburger? I mean when click on hamburger only show "list 1" without showing "list 2" until you click on "list 1".
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$(".primary-list").slideToggle("fast");
});
});
$(document).ready(function() {
$(".primary-list").click(function() {
$(".standard-list").slideToggle("fast");
});
});
</script>
</head>
<body>
<button>☰</button>
<ul>
<li class="primary-list">list 1
<ul>
<li class="standard-list">list 2
</li>
<li class="standard-list">list 2
</li>
<li class="standard-list">list 2
</li>
</ul>
</li>
<li class="primary-list">list 1
</li>
<li class="primary-list">list 1
</li>
<li class="primary-list">list 1
</li>
<li class="primary-list">list 1
</li>
</ul>
</body>
</html>
Just add $(".standard-list").slideToggle("fast"); at first in the document.ready() function.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".standard-list").slideToggle("fast");
$("button").click(function() {
$(".primary-list").slideToggle("fast");
});
});
$(document).ready(function() {
$(".primary-list").click(function() {
$(".standard-list").slideToggle("fast");
});
});
</script>
</head>
<body>
<button>☰</button>
<ul>
<li class="primary-list">list 1
<ul>
<li class="standard-list">list 2
</li>
<li class="standard-list">list 2
</li>
<li class="standard-list">list 2
</li>
</ul>
</li>
<li class="primary-list">list 1
</li>
<li class="primary-list">list 1
</li>
<li class="primary-list">list 1
</li>
<li class="primary-list">list 1
</li>
</ul>
</body>
</html>
first - use just one $(document).ready();
second - use $(this)
3rd - its better to use .on(); its avoid any errors you will may facing
4th - you can use display: none; for your sub-menu css or add $(".standard-list").hide(); to hide your sub-menu on load
<script>
$(document).ready(function() {
$("button").on('click',function() {
$(".primary-list").slideToggle("fast");
});
$(".primary-list").on('click',function() {
$(this).find('ul').slideToggle("fast");
});
});
</script>
JSFIDDLE
The problem is that standard-list is inside of primary-list, so when you click on standard, you also click on primary. You need the class on something that is not wrapped around the standard list.
<li class="primary-list"><a class="toggle-standard" href="#">list 1</a>
<ul>
<li class="standard-list">list 2
</li>
<li class="standard-list">list 2
</li>
<li class="standard-list">list 2
</li>
</ul>
</li>
$(document).ready(function() {
$("button").click(function() {
$(".primary-list").slideToggle("fast");
});
});
$(document).ready(function() {
$(".toggle-standard").click(function() {
$(".standard-list").slideToggle("fast");
});
});
Fiddle: https://jsfiddle.net/Forklift/7w73otch/
I have two navigations and i want the browser to load #informals-1 by default on page load. How can I do that using javascript? I have a code to navigate between the -nav and -subnav, but the #informals-1 section is not activated by default. I have to click on the informals-1 anchor to get the subnav under it.
Before activation - this
After activation - this
<nav id="documentation-nav">
<ul>
<li><span>1</span>Informals
</li>
<li><span>2</span>Tech Events</li>
</ul>
</nav>
<nav id="documentation-subnav">
<ul id="informals">
<li><a href="#informals-1" >Tic Toc</a></li>
<li><a href="#informals-2" >Jack of all Trades</a></li>
<li><a href="#informals-3" >Tattoo making</a></li>
<li><a href="#informals-4" >Face painting</a></li>
<li><a href="#informals-5" >Foosball</a></li>
<li><a href="#informals-6" >Solo Impromptu</a></li>
<li><a href="#informals-7" >Challenge accepted</a></li>
<li><a href="#informals-8" >Sack Race</a></li>
<li><a href="#informals-9" >Connected</a></li>
<li><a href="#informals-10" >Mystery date</a></li>
<li><a href="#informals-11" >The 90's Game</a></li>
</ul>
<ul id="techevents">
<li>name1</li>
<li>name1</li>
<li>name1</li>
<li>name1</li>
<li>name1</li>
</ul>
You can do something like the following.
Give the li an id
<li id="yourId"><a href="#informals-1" >Tic Toc</a></li>
then have the window scroll to 500 pixels from top. Change 500 to whatever amount is correct for you.
document.getElementById('yourId').style.display = 'block';
window.scrollTo(0, 500);