Navigation dropdown toggle - mouseout as well as off hover? - javascript

Can someone help me adapt the following so that the dropdown menu not only hides on click, but also hides on mouseout and/or when another of the top level menu buttons is hovered on?
jsfiddle
$(document).ready(function () {
$("li").click(function () {
$('li > ul').not($(this).children("ul").toggle()).hide();
});
});
Still getting my feet wet with jQuery/script coding.
NOTE: I'm using divs as part of the structure of the dropdown, as in the instance that "ul" above is replaced by a div.
FYI, I can't take credit for the above, it is the work of Pramod Sankar L (user PSL).
Any help would be appreciated!

Try
.mouseleave()
:has()
$(document).ready(function () {
$("li:has(ul)").click(function () {
$('li > ul').not($(this).children("ul").toggle()).hide();
}).mouseleave(function () {
$('li > ul').hide();
});
});
$("li:has(ul)") select li which contains ul
Fiddle Demo
Updated After Op's Comment
$(document).ready(function () {
$("#dropmenu li:has(div)").click(function () {
$('#dropmenu li.second-level > #dropmenu li.second-level div.drop_6col-bottom').not($(this).children("#dropmenu li.second-level div.drop_6col-bottom").toggle()).hide();
}).mouseleave(function () {
$(this).children('div').hide();
});
});

Related

drop downlist when I click outside close

iam using this dropdown list plugin;
https://tympanus.net/Development/SimpleDropDownEffects/index4.html
when I click outside close, i want the dropdown list to close.
its js doc.;
view-source:https://tympanus.net/Development/SimpleDropDownEffects/js/jquery.dropdown.js
thank you all.
on you click ul change height and add class cd-active
take easy remove class and height
$('.cd-dropdown').blur(function () {
$(this).removeClass('cd-active');
$('.cd-dropdown ul').css('height', 'auto');
});
or
$(document).on('blur','.cd-dropdown',function () {
$(this).removeClass('cd-active');
$('.cd-dropdown ul').css('height', 'auto');
});
This plugin does not have this option. But you can use the ascent of the event. A story on the end element of the handler that will check if there are any items on the list that have been clicked, and then close it
Look at my example...
codepen.io/Hydrock/pen/zPyjQG
try this
$('.cd-dropdown').focusout(function () {
$(this).removeClass('cd-active');
$('.cd-dropdown ul').css('height', 'auto');
$('.cd-dropdown ul li').css('top', '0px');
});
$('.cd-dropdown').focusout(function () {
$(this).removeClass('cd-active');
$('.cd-dropdown ul').css('height', 'auto');
$('.cd-dropdown ul li').css('top', '0px');
});
in my js file not like this.
$(this).removeClass('cd-active'); xx
Like this
$(this).removeClass('cd-actives');
I have changed before, noticed afterwards
thank you solved

Jquery selector - affecting element inside same DOM element

My CMS generates menus as lists without any id's and classes. For submenus, there are nested lists.
I made jquery script for expanding submenus:
$(function () {
$(".wrapper ul li").click(function () {
if ($(this).has("ul").length) {
$("a", this).removeAttr('href');
$("ul", this).slideToggle();
}
});
});
My problem is that this script reacts to clicking whole li area and I want it to react to clicking link inside li. Of course I just have to add "a" to selector making it ".wrapper ul li a" but what about condition checking if there is ul nested inside li? And slidetoggle selector. How should I change these?
This might work with minimal modification to your original code.
$(function () {
$(".wrapper ul li a").click(function ()
{
this = $(this).parent();
if ($(this).has("ul").length) {
$("a", this).removeAttr('href');
$("ul", this).slideToggle();
}
}
);
});
Test and see if it works. I have not tested it. Cheers

How to keep next() function from hiding on hover?

I have a simple jquery menu and I am trying to keep the submenu visible if a user hover overs it. so that I can select it if needed. However, when I get off the hover element the submenu will hide. Obviously, that's what I want as long as it's not also hovering over the submenu.
$(document).ready(function () {
$('.mainBar li a').hover(function(){
$(this).next().show() }, function () {
$(this).next().stop().hide()
});
});
http://jsfiddle.net/azxRX/1/
My opinion is to create this menus with css. Anyway i change a bit to this:
$('.sideBar > ul > li').bind('mouseover', openSubMenu);//This line sets it up so that when the mouse is moved over a li in myMenu, the function openSubMenu is called
$('.sideBar > ul > li').bind('mouseout', closeSubMenu);//This do exacly the same with the above but binds on mouseout instead.
function openSubMenu() {
///when the mouse rolls over the list item,
///the function looks for an unordered list within it.
///If one is found, it sets the style property display to block
$(this).find('ul').css('display', 'block');
};
function closeSubMenu() {
///This one does the oposite of openSubMenu function
$(this).find('ul').css('display', 'none');
};
fiddle
You can do this instead:
$(document).ready(function () {
$('.mainBar li').mouseover(function () {
$(this).find('.subBar').show();
console.log('over');
});
$('.mainBar li').mouseout(function () {
$(this).find('.subBar').hide();
});
});
This is the jsfiddle

Responsive tabs to accordion issue

I'm in the process of modifying a responsive tabs to accordion, please see the jsfiddle
When the current 'active' tab is clicked it hides which I understand is what it is meant to do in the code, however I would like it if this does not happen and doesn't hide. Here is the current javascript:
$('#nav').children('li').first().children('a').addClass('active')
.next().addClass('is-open').show();
$('#nav').on('click', 'li > a', function() {
if (!$(this).hasClass('active')) {
$('#nav .is-open').removeClass('is-open').hide();
$(this).next().toggleClass('is-open').toggle();
$('#nav').find('.active').removeClass('active');
$(this).addClass('active');
} else {
$('#nav .is-open').removeClass('is-open').hide();
$(this).removeClass('active');
}
});
It's basically just applying classes dependent on what is clicked and what is currently active or not. I guess I need to change the logic of this?
If what I understood is correct that you want to stop hiding the active div when clicked again. Just remove the else part...
$('#nav').children('li').first().children('a').addClass('active')
.next().addClass('is-open').show();
$('#nav').on('click', 'li > a', function() {
if (!$(this).hasClass('active')) {
$('#nav .is-open').removeClass('is-open').hide();
$(this).next().toggleClass('is-open').toggle();
$('#nav').find('.active').removeClass('active');
$(this).addClass('active');
}
});
Check this Fiddle

jQuery Mobile Alert if navbar ID is Clicked

I am trying to revise the following jQuery Mobile code to display if the navbar with the id "e" is clicked. The code below works for all of the navbar items, but I only want the alert to appear if the navbar with the id "e" is clicked. Any help revising this would be great. Thanks much!
$(document).on('pagebeforeshow', '#p_page', function(event){
$('div[data-role="navbar"] a').live('click', function () {
alert("div E clicked");
});
});
This should work:
$(document).on('pagebeforeshow', '#p_page', function(event){
$('div[data-role="navbar"] ul li a#e').on('click', function () {
alert("div E clicked");
});
});
Working example: http://jsfiddle.net/Gajotres/rnRqm/
Instead of live use click event because live is deprecated from jquery.
$('div[data-role="navbar"] a').on('click', function () {
alert("div E clicked");
});

Categories