I have a navigation menu, when i hover over the links i want to show each links sub menu then toggle or hide and show each individual one.
At the moment when I hover over the link on the navigation menu it displays all the sub menus for all the links.
I have attached a fiddle with a demo of my code so far: -
http://jsfiddle.net/QTm2c/1/
Here is the jQuery
$(document).ready(function() {
$("li.navmain__item").hover(function () {
$("span.navmain__item--subnavholder").toggle();
})
})
Thanks
Use :
$(document).ready(function() {
$("li.navmain__item").hover(function () {
$(this).children("span.navmain__item--subnavholder").toggle();
});
});
Fiddle
Try this:
$(document).ready(function() {
$("li.navmain__item a").hover(function () {
$(this).siblings().toggle();
})
})
You have to target the specific submenu. Inside the hover() handler, use:
$("span.navmain__item--subnavholder", this.parentElement).toggle();
You just have to get the subnavholder for the element you're hovering. In the callback, the element you're hovering over is stored in this (as an HTML Element).
Changing
$("span.navmain__item--subnavholder").toggle();
to
$(this).parent().find("span.navmain__item--subnavholder").toggle();
Will do the trick!
In your current setup this should do it
$(document).ready(function() {
$("li.navmain__item").hover(function () {
$(this).find(".navmain__item--subnavholder").toggle();
})
})
You could also add indexing to the script. Here is edited, working jssfiddle: http://jsfiddle.net/QTm2c/8/
$(document).ready(function() {
$("li.navmain__item a").hover(function () {
var index = $( "li.navmain__item a" ).index( this );
$("span.navmain__item--subnavholder"+index).toggle();
})
})
Related
On clicking .cart-contents-toggle the #cartplus-dropdown div should slide down and at the same time slide up without clicking on it a second time. Here is the URL where this is implemented: Website Link
jQuery(document).ready(function() {
jQuery('#cartplus-dropdown').hide();
jQuery('.cart-contents-toggle').on("click", function() {
jQuery('#cartplus-dropdown').slideToggle();
});
});
Please use the updated code below and let me know if you have any issue or query.
jQuery(document).ready(function() {
jQuery('.cart-contents-toggle').on("click", function() {
jQuery('#cartplus-dropdown').toggle();
jQuery('#cartplus-dropdown').slideToggle();
});
});
Hope this may be helpful to you.
Tried this and working now
jQuery('.c-btn').mouseenter(function(e) {
jQuery('.cartplus-dropdown').slideDown();
e.stopPropagation();
});
jQuery('.c-btn').mouseleave(function(e) {
jQuery('.cartplus-dropdown').slideUp();
e.stopPropagation();
});
I have a bit of javascript. I want to achieve that when you click on the menuBtn class that it changes 'nav' to fixed fixed.
// menu animation
$(window).load(function() {
$('.menuBtn').click(function(e) {
e.preventDefault();
(this.classList.contains('is-active') === true) ? this.classList.remove('is-active'): this.classList.add('is-active');
$('nav').slideToggle();
});
});
$('.menuBtn').click(function() {
//nav position fixed//
});
$('.menuBtn').click(function() {
$('.nav').css('position','fixed');
});
Not quite sure if nav is an ID or a class. # or . the code should look like this.
I have a menu with a dropdown, where the LI element gets an activeclass on click. I have, after a lot if struggle, managed to set a script that sets an active class to an div that I have hidden, which shows on the click as an overlay of the site (under the dropdown). everything works as It should, except if I click outside the dropdown to close it instead of clicking the menubutton. This doesnt change my overlay div's class- how do I change my script to work on clicks outside the dropdown aswell, and what should I target here?
The hidden div:
<div id="site-overlay"></div>
script:
$.noConflict();
jQuery(document).ready(function(){
jQuery('li').on('click', function(){
if(jQuery(this).hasClass('active')) {
jQuery("#site-overlay").addClass("active");
} else {
jQuery("#site-overlay").removeClass("active");
}
})
});
This will work:
$.noConflict();
jQuery(document).ready(function () {
jQuery('li').on('click', function () {
if (jQuery(this).hasClass('active')) {
jQuery("#site-overlay").addClass("active");
} else {
jQuery("#site-overlay").removeClass("active");
}
});
jQuery("#site-overlay").click(function () {
jQuery(this).removeClass("active");
});
});
Notice the new event handler.
You can add a click event to the document body and then check the click location:
$(document).click(function(event) {
if(!$(event.target).closest('#site-overlay').length) {
if($('#site-overlay').is(":visible")) {
$('#site-overlay').hide()
}
}
})
jQuery('li').on('click', function(){
...
}
This executes only when you click on 'li' element.
But what about clicking outside 'li' element?
Try to add or replace:
jQuery('body').on('click', function(){
if(jQuery('.megamenu li').hasClass('active')) {
jQuery("#site-overlay").addClass("active");
} else {
jQuery("#site-overlay").removeClass("active");
}
}
I have menu with 2 submenus. Using jQuery I want to higlight hovered item. I can't solve how to higlight parent item, when cursor is on the child item. For hovering I used class caled active:
.vertical-active {
background:#0F6;
}
Jquery function looks like this:
$(document).ready(function (e) {
$('.submenu a').hover(
function () {
$(this).addClass('vertical-active');
$(this).parent('vertical-links a').addClass('vertical-active');
},
function () {
$(this).removeClass('vertical-active');
$(this).parent('vertical-links a').removeClass('vertical-active');
});
});
Problem is in parent selector, but I don't know how to select submenu's parent item.
JSFiddle link:http://jsfiddle.net/6g9tZ/4/
$(document).ready(function() {
$('.submenu a').on('mouseenter mouseleave', function() {
$(this).add($(this).closest('ul').closest('li').children('a')).toggleClass('vertical-active');
});
});
FIDDLE
EDIT:
to highlight the parent as well, you'd do
$('.vertical-links > li > a').on('mouseenter mouseleave', function() {
$(this).toggleClass('vertical-active')
});
FIDDLE
Use .siblings in addition to .closest.
FIDDLE
$(document).ready(function (e) {
$(".vertical-links > li > a").on("mouseenter mouseleave", function(){
$(this).toggleClass('vertical-active');
});
$('.submenu a').on("mouseenter mouseleave",function () {
$(this).toggleClass('vertical-active');
$(this).closest("ul").siblings("a").toggleClass('vertical-active');
});
});
Replace the relevant parts of your code with:
$(this).parents('li:eq(1)').find("> a").addClass('vertical-active');
....
$(this).parents('li:eq(1)').find("> a").removeClass('vertical-active');
One problem with your code is that you were looking for a "parent <a>", but there is no such thing; the <a> is a child of your parent. So here we search for a parent <li>, not the immediate, but actually the grandparent, find its direct <a> child and highlight it.
Additionally you had parent('vertical-links') which should be parent('.vertical-links') (not the dot: its a class not an element).
i am using jquery for set background color to the table data and its working fine but i need when user again click the td the color should be deselect. its my script for add color.
java script:
jQuery('td').click(function () { $(this).addClass('active'); });
my css class:
.active{background-color:red;}
when user again click the td the class should remove. How to achieve this.
jQuery('td').click(function () { $(this).toggleClass('active'); });
toggleClass adds if it doesn't exist or removes if it does exist.
You can use
$(this).removeClass('active');
although you would need to do a check to see if it is already active, which would make your code look like this:
jQuery('td').click(function () {
if($(this).hasClass('active') {
$(this).removeClass('active');
} else {
$(this).addClass('active');
}
});
EDIT:
#Justice is more correct:
jQuery('td').click(function () { $(this).toggleClass('active'); });