In a jQuery setting what is the best way to stop default propagation on the first click and show the submenu if the user is on the first click then restore the a link on the second click, or hide the currently displayed submenu if the users clicks on any other part of the page i have tried this with a few variations but none seem to work:
$('.nav-bar li:has(ul)').on('click', function () {
var CheckForClasssub = $('.nav-bar li ul').hasClass('subshow');
if (CheckForClasssub) {
$('.subshow').hide('slow').removeClass('subshow');
} else {
$('.nav-bar li').on('click', function () {
var thisflyout = $(this).find('.flyout');
$(thisflyout).toggle('slow').addClass('subshow');
});
}
});
this is my current menu structure:
<ul class="nav-bar">
<li>
google
<ul>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>
</li>
<li>google</li>
<li>google</li>
<li>google</li>
<li>google</li>
</ul>
currently clicking on class="flyout" toggles the sub menu, but i would love to use the whole link
/*UPDATE**/
Ok this works fine to display on first click then follow link on second but it doesn't hide if the page is clicked anywhere else...
$('.nav-bar li:has(ul)').on('click', function (e) {
var CheckForClasssub = $('.nav-bar li ul').hasClass('subshow');
if (CheckForClasssub) {
$('.subshow').hide('slow').removeClass('subshow');
} else {
var thisflyout = $(this).find('.flyout');
e.preventDefault();
$(thisflyout).show('slow').addClass('subshow');
}
});
/*UPDATE**/
To clarify requirments:
user clicks on menu item with both an A link as the parent and a submenu using
ul > li + a > ul > li
If this is the first click show submenu.
If second click direct through to the relvant a link.
If user has clicked once on menu item 1 showing the submenu but decided to click on another parent menu item with a submenu hide submenu 1 show submenu 2.
If user has clicked on menu item 1 and decides to click anywhere else on page that isn't a menu item with a submenu hide the open submenu....
var submenucheck = $('.nav-bar li ul');
var submenucheckli = $('.nav-bar li a');
showHide = false;
function checkforsubsopen() {
// body...
$.each(submenucheck, function(el) {
/* iterate through array or object */
if($(this).hasClass('subshow')){
showHide = true;
}
});
return showHide;
}
$('.nav-bar li:has(ul)').on('click', function(e) {
var CheckForClasssub = $('.nav-bar li ul').hasClass('subshow');
if(CheckForClasssub){
}
else{
var thisflyout = $(this).find('.flyout');
e.preventDefault();
$(thisflyout).show('slow').addClass('subshow');
}
});
$(document).on('click', function(){
checkforsubsopen();
if(showHide){
$('.subshow').hide('slow').removeClass('subshow');
}
console.log(showHide);
});
Related
I have a set of 3 tabs and everytime i click on the same tab it replays the fade-in animation and blinks and i need it to only show the animation when i click on a new tab and not display the fade-in animation when i click on the same tab.
Its because it removes and re-adds the same class everytime i click at it.
what i currently have is:
(function ($) {
var tabs = $(".tabs li a");
tabs.on("click", function () {
var content = this.hash.replace("/", "");
tabs.removeClass("active");
$(this).addClass("active");
$("#content").find("section").hide();
$(content).fadeIn(200);
});
})
<ul class="tabs">
<li><a class="active" href="#/one">Tab 1</a></li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
What i have tried:
// if tab is clicked/selected then remove animation
if(!$(tabs).data("clicked")) {
$(content).fadeIn(200);
} else {
$(content).fadeIn(0);
}
$(".active").data('clicked', true);
// if click count is higher than 1 then remove animation
var trigger = $(this),
clickCount = trigger.data('clickCount');
clickCount++;
trigger.data('clickCount', clickCount);
if(clickCount > 1) {
$(content).fadeIn(0);
}
Have you tried like that :
var tabs = $(".tabs li a");
tabs.on("click", function () {
if( !$(this).hasClass("active") ){
var content = this.hash.replace("/", "");
$("#content").find("section").hide();
$(content).fadeIn(200);
$(".tabs li").find(".active").removeClass("active");
$(this).addClass("active");
}
});
I'd suggest always check if active class exists before adding it.
And only if it doesn't exist add this class and animation.
In this case if active class exists you will not be able to add this class again and the animation not be triggered again.
in JavaScript
element is the button you need specifically.
you can get the button id using a simple Js query
var element = document.getElementById('buttonID');
element.on('click', (){
let counter = 0;
return function inner() {
counter++;
console.log('Number of clicks: ' + counter);
};
})
I'm using a js code for additional menu options on click, but how can I keep the "click menu" permanently open, say after one of its option has been clicked, so the user won't have to start over again once taken to the new page.
HTML:
<div class="c3" id="topLeftNav">
<ul class="secnav" id="secnav">
<h6>Generelt</h6>
<li class="current">Ordklasser
<ul class="ternav">
<li>Substantiver</li>
<li>Verber</li>
<li>Adjektiver</li>
<li>Numeralier</li>
<li>Interjektioner</li>
</ul>
And my JS:
///ternav/secnav -- catch inner clicks (ternav) first and preventDefault.
$('.ternav li a').click(function () {
window.location = $(this).attr('href');
});
//secnav
$('li').click(function (event) {
if ($(this).has('ul').length) {
event.preventDefault();
if ($(this).find('ul').css("display") == "none") {
$(this).find('a').eq(0).addClass('current');
$(this).find('ul').slideDown();
}
else {
$(this).find('ul').slideUp();
$(this).find('a').eq(0).removeClass('current');
}
}
});
In mobile view of my page, when the menu option is selected, it drops down and an expanded class value is added. That value never gets removed once I click on a menu element. I need to remove the expanded class value when any item in the menu is clicked and remove the expanded menu since it covers the entire page of my one page site.
check in mobile view.
<nav class="top-bar" data-topbar="" style="position: fixed; top: 0px;">
.
.
.
</nav>
<script type="text/javascript">
$(document).ready(function () {
$(document).click(function (event) {
var clickover = $(event.target);
var _opened = $(".expanded").hasClass("expanded in");
if (_opened === true && !clickover.hasClass("navbar-toggle")) {
$("button.navbar-toggle").click();
}
});
});
</script>
You can do something like this.
You can remove that "expanded" class from the "top-bar" for each menu item click by checking that class is exist.
$(".top-bar-section ul li > a").click(function() {
if($('.top-bar').hasClass('expanded')){
$('.top-bar').removeClass('expanded')
}
});
Use $('#menuContent').toggleClass('expanded') when the menu button is clicked, and $('#menuContent').removeClass('expanded') when a menu item is clicked, like so:
$(document).ready(function() {
$('#menuButton').click(function(event) {
$('#menuContent').toggleClass('expanded');
});
$('.menuItem').click(function(event) {
$('#menuContent').removeClass('expanded');
});
});
Here is a fully functional JSFiddle to demonstrate.
I have a couple nested & hidden sub-nav lists
<ul class="nav">
<li>Home</li>
<li><a class="profile" href="#">Profile</a>
<ul id="profile">
<li>Company</li>
<li>Structure</li>
<li>Team</li>
</ul>
</li>
<li><a class="projects" href="#">Projects</a>
<ul id="projects">
<li>Chapter</li>
<li>Pblc Trde</li>
<li>Globe</li>
<li>Komforte</li>
</ul>
</li>
I am currently using some jQuery i found online to show/hide the sub-nav upon click. What I am trying to accomplish is:
Hopefully clean up the show/hide click function of the sub-nab menus.
When clicking on the sub-nav menu items, the corresponding page that opens, needs to have the sub-nav expanded and give the corresponding menu item active class, so as to let the user know which page they are on.
I am hoping to do this purely in JS/jQuery. The installation of the site will be in WordPress.
$(document).ready(function () {
$(".profile").click(function () {
var X = $(this).attr('id');
if (X == 1) {
$("#profile").hide();
$(this).attr('id', '0');
} else {
$("#profile").show();
$(this).attr('id', '1');
}
});
//Mouse click on nav
$("#profile").mouseup(function () {});
//Document Click
$(document).mouseup(function () {
$("#profile").hide();
$(".profile").attr('id', '');
});
$(".projects").click(function () {
var X = $(this).attr('id');
if (X == 1) {
$("#projects").hide();
$(this).attr('id', '0');
} else {
$("#projects").show();
$(this).attr('id', '1');
}
});
//Mouse click on nav
$("#projects").mouseup(function () {});
//Document Click
$(document).mouseup(function () {
$("#projects").hide();
$(".projects").attr('id', '');
});
});
window.onload = function () {
$("ul#profile li:first").addClass("active");
};
$(document).ready(function () {
$("ul#profile").show()
});
$(document).ready(function()
{
// Get the name of the page. Split the URL at the '/':s and get the last part
// with pop():
var pageName = (location.pathname).split('/').pop();
// If we couldn't get a page name, default to index.html:
if( pageName == '' )
{
pageName = 'index.html';
}
// Hide ul:s that are children of the navigation:
$('.nav ul').hide();
// Event handler for clicks on navigation links:
$('.nav a').on('click', function()
{
// Change visibility for the first ul-child of the current li.
// $(this) refers to the clicked element.
$(this).parent('li').find('ul').first().toggle();
// Hide other sub-menus:
$(this).parents('li').siblings('li').children('ul').hide();
});
// Search through all link elements in the nav menu:
$('.nav').find('a').each(function(index, value)
{
// Append a '$' to the pagename to make the match()-function search
// from the end of the href value:
pageName += '$';
if( value.href.match(pageName))
{
// If the pagename matches the href-attribute, then add the 'active'
// class to the parent li, and show parent ul:s:
$(this).parent('li').addClass('active').parents('ul').show();
}
});
});
You could use a Cookie to hold the value of the currently open menu. This will allow for the value to be saved/retrieved between page loads and browser sessions.
As you've already got jQuery setup you can use the jQuery Cookie plugin to simplify things.
The code for it is quite simple (more examples on plugin page).
$.cookie('open_menu', 'projects'); //Save 'projects' under 'open_menu'
$.cookie('open_menu') //Returns 'projects'
Just check the value on page load and save it when one of the menu's is clicked.
If you'd prefer not to add any extra plugins here's some documentation on JavaScript's inbuilt cookie API.
Edit: I've created a JSFiddle with an example for you. The Cookie code doesn't seem to work in there sandbox, but the code should work for you, let me know if you have any troubles.
$(window).load(function() {
if ($.cookie('show_menu') !== undefined) {
$('#' + $.cookie('show_menu')).click();
}
$('.nav > li > ul').each(function () {
//Hide the sub lists
$(this).hide();
//Get link with same ID as Class
var id = $(this).attr('id');
//When link is clicked
$('.' + id).click(function () {
//Get the sub list
var list = $('#' + $(this).attr('class'));
//Check if it's currently visible
if (list.is(':visible')) {
list.hide(); //Hide list
$.cookie('show_menu', ''); //Unset open menu
} else {
$('.nav > li > ul').hide(); //Hide all other lists
list.show(); //Show list
$.cookie('show_menu', list.attr('class')); //Set open menu
}
});
});
});
I have a dropdown menu that slides up and down when toggled to reveal it's menu items. The problem is that when i go to click one of the menu items after the menu has slid down, it is recognized as a toggle and the menu slides up without opening the link of the menu item.
HTML
<li class="dropdown">
Carbohydrates, proteins & fats
<ul>
<li>Carbohydrates</li>
<li>Proteins</li>
<li>Fats</li>
</ul>
</li>
dropdown script:
$(document).ready(function () {
$('.dropdown').toggle(
function () {
//show its submenu
$('ul', this).slideDown(300);
},
function () {
//hide its submenu
$('ul', this).slideUp(300);
}
);
});
I'd appreciate any help with this.
Try moving the triggering link outside of the dropdown items
Carbohydrates, proteins & fats
<div class="divdropdown">
<ul>
<li>Carbohydrates</li>
<li>Proteins</li>
<li>Fats</li>
</ul>
</div>
And then slightly modify your jquery
$(document).ready(function () {
$('.dropdown').toggle(
function () {
//show its submenu
$('ul', $(".divdropdown")).slideDown(300);
},
function () {
//hide its submenu
$('ul', $(".divdropdown")).slideUp(300);
}
);
});
Why are you individually closing/opening the menu items? Everything gets hidden anyway when the parent <li> element is hidden -- why do anything after that?
I think you want to disable event bubbling.
EDIT 1: You're over-complicating things. Try this:
$(document).ready(function () {
$('.dropdown').toggle();
});
EDIT 2: Can you be more specific about what you want? If you want a particular element, when clicked, to control the motion of a particular list, try this:
$(document).ready(function () {
$('a.someLink').click(function() {
$('.dropdown').toggle();
});
});
Unfortunately none of the above answers were working for me. I found a solution shortly afterwards.
HTML
<li class="dropdown">
<a class="disablelink" href="#">Carbohydrates, proteins & fats</a>
<ul class="sub_navigation">
<li>Carbohydrates</li>
<li>Proteins</li>
<li>Fats</li>
</ul>
</li>
jquery script
$(document).ready(function() {
$('.dropdown').click(function() {
// When the event is triggered, grab the current element 'this' and
// find it's children '.sub_navigation' and display/hide them
$(this).find('.sub_navigation').slideToggle();
});
$(".disablelink").click(function(e) {
e.preventDefault();
});
});
That solution is working perfectly for me. I added in e.preventDefault(); to stop the page jumping up and down when i clicked on the link.