Not sure if I'm doing this right. I have a menu that toggles between open and close. Now I want a button to show up if the menu hasClass("open");. and I would like to remove the class once the class open is gone.
Here is the HTML part;
<nav>
<a class="closed"><i class="fa fa-bars"></i>Menu</a>
<ul>
<li>Menu items</li>
</ul>
<div class="close-btn">
<i class="fa fa-times" aria-hidden="true"></i>
</div>
</nav>
The HTML works, so this is not that important, the important part is that the elements are: nav, a, closed.
Now for the jQuery part;
$(document).ready(function(jQuery) {
if ($("nav a").hasClass("open")) {
$(".close-btn").addClass('show-me-the-button');
} else {
$(".close-btn").removeClass('show-me-the-button');
}
});
But this doesn't work. I've searched stackoverflow but all gave me this code... not sure why it isn't working. I get no script errors... any help?
You check classes only on initial page load. Where do you change open class? You should move this code in that place.
User - oryol is correct
Try to put your code inside a handler like this:
$(document).ready(function(jQuery) {
$('nav a').on('click', function(e) { // <----------- Click Handler (jQuery)
// Use $(this) to access current clicked element
if ($(this).hasClass("open")) {
$(".close-btn").addClass('show-me-the-button');
} else {
$(".close-btn").removeClass('show-me-the-button');
}
})
});
See more about Click Handler in jQuery
Hope this helps!
Related
I have a list item (#planAdminMenuItem) that has an onclick attribute. This list item has an icon inside of it (.spinner) that will collapse #collapseExample. Whenever the .spinner is clicked, I want it to run bootstrap collapse only. I do not want it to run drawPlanAdmin function. I have tried adding event.stopPropagation to my toggleSpinnerLeftMenu function, but whenever I do that, it also stops the bootstrap collapse. The parent click is blocked, but so is bootstrap collapse.
THE PHP & HTML CODE
<ul>
<li id="planAdminMenuItem" onclick="plan.drawPlanAdmin();">
Book Plan
<span class="icn icn-chevron-down spinner" onclick="ui.toggleSpinnerLeftMenu(this,event);" data-toggle="collapse" data-target="#collapseExample" data-aria-expanded="true" aria-controls="collapseExample"></span>
</li>
<!-- the collapsable area -->
<li id="collapseExample" class="collapse in">
<ul>
<li onclick="plan.drawRunListAdmin();">
Run List View
</li>
<li onclick="plan.drawLadderAdmin();">
Ladder View
</li>
</ul>
</li>
</ul>
THE JS CODE
toggleSpinnerLeftMenu:function(el,event){
el = jQuery(el);
if(el.hasClass('icn-chevron-up')){
el.addClass('icn-chevron-down');
el.removeClass('icn-chevron-up');
}else if(el.hasClass('icn-chevron-down')){
el.addClass('icn-chevron-up');
el.removeClass('icn-chevron-down');
}
event.stopPropagation(); //why is this stopping the collapse also?
},
stopPropagation is doing exactly what it is meant to do.
If you want the parent element to be propagated by the click on the inner element then simply don't do event.stopPropagation at all.
Though for some reasons if you need to have that then my suggestion is: call the function like
toggleSpinnerLeftMenu:function(el,event){
el = jQuery(el);
if(el.hasClass('icn-chevron-up')){
el.addClass('icn-chevron-down');
el.removeClass('icn-chevron-up');
}else if(el.hasClass('icn-chevron-down')){
el.addClass('icn-chevron-up');
el.removeClass('icn-chevron-down');
}
plan.drawPlanAdmin(); // Call the function inside of the child element's click handler.
event.stopPropagation(); //why is this stopping the collapse also?
},
Update: Since you described the issue more clearly in the comment, which has a solution completely south of what I've written above, I am updating with the new content that may be able to help.
Instead of attaching two event handlers, one using an inline onClick attribute and another using Bootstrap's data-collapse, use one:
$(".spinner").on("click", function(event) { // tip: Give a better id or class name
$('#collapseExample').collapse({toggle: true});
ui.toggleSpinnerLeftMenu(this, event);
});
This is the general idea of doing this, you may still have to make some adjustments to your method calls to fit it in.
I'm using the responsive menu script from PureCss example. I'd like for the menu to disappear after the user clicks a link. How could I automatically hide the menu after a click has been made?
I've tried to use hide() or .fadeOut(), but it doesn't work
https://jsfiddle.net/bpvm8b5L/3/
Add an Event Listener to your anchor elements inside the menu that on click trigger a function that reproduce the action of when the hamburger menu icon is pressed, so it removes the class active from
<div id="layout" class=" active">
…
<div id="menu" class='active'>…</div>
So try to add in your ui.js file, inside your main function:
var ulElem = document.getElementById('ulElem');
ulElem.onclick = function (e) {
e.preventDefault();
console.log(e.target.classList.value);
if(e.target.classList.value === "pure-menu-link") {
toggleClass(layout, active);
toggleClass(menu, active);
toggleClass(menuLink, active);
}
}
And remember to set the id name of the ui element on your HTML file as id=uiElem.
You can as well try this
In html, assuming you have something like this:
<nav id="toggleMenu">
//all your links
</nav>
In jquery,
$('#toggleMenu li a').click(function(){
$('#toggleMenu).hide(200);
});
I have small dropdown profile menu with logout button etc. I need to show the menu when I click on the button and hide it when i click anywhere on page or on the button as well.
<div id='menu'>
<ul>
<li class='has-sub'> <a class="testbutton" id="userButton" onclick="dropdown()" href="#">
<span id="buttonText">User name</span> <span id="triangleDown">▾</span>
</a>
<ul id="submenu">
<li class='has-sub'><a href='#'><span>Change password</span></a>
</li>
<li class='has-sub'><a href='logout.php?action=0'><span>Logout</span></a>
</li>
</ul>
</li>
</ul>
</div>
I used JavaScript. At this time menu is displayed on hidded only when I click on profile button. I also know how to start function using something like document.ready.
My not working code:
function dropdown() {
if ($('#submenu').css('visibility') == 'hidden') {
$('#submenu').css('visibility', 'visible');
} else {
$('#submenu').css('visibility', 'hidden');
}
};
$(document).click(function (event) {
if ($('#submenu').css('visibility') == 'visible') {
$('#submenu').css('visibility', 'hidden');
}
});
But when I combine this methods it does not works. So when I clicked on the button to open menu, nothing happened.
Sorry for my English :)
Thanks for help in advance.
This has partly to do with something called event propagation. Put simply, this means that click events will register not only on the clicked element, but also on any parent or ancestor elements of that element.
So if you click a DIV, the event will also be registered on the BODY, because the DIV is inside the BODY. Put abstractly, if a kitchen is the scene of a crime, then the apartment that houses that kitchen is also the scene of a crime. One is inside the other.
This is prevented by preventing propagation - in jQuery, by running the stopPropagation() method of the evt object that is automatically passed to your event handler.
In any case, your situation can be greatly simplified.
var menu = $('#menu'), but = $('#menu_button');
$(document).on('click', '*', function(evt) {
evt.stopPropagation(); //<-- stop the event propagating to ancestral elements
if ($(this).is(but)) //<-- on button click, toggle visibility of menu
menu.toggle();
else if (!$(this).closest(menu).length) //<-- on click outside, hide menu
menu.hide();
});
Assumption: I have assumed that the toggler button is targetable via the selector '#menu_button'. Update this as required. Also, the code should run inside a DOM-ready handler.
The code listens for clicks to any element. If it's registered on the button, the visible state of the menu is toggled. If it's to an element outside of the menu, the menu is hidden. (If, in the latter case, the menu is already hidden, this will have no effect.)
Here's a working JS Fiddle that demonstrates the approach.
Try this:
$(function() {
$('.test-button').click(function(event) {
event.stopPropagation();
$('#submenu').toggle();
});
$('body').click(function() {
var submenu = $('#submenu');
if(submenu.is(":visible")) {
submenu.hide();
}
})
});
Im trying to create a false hover event for my site using jQuery...
I have created the following only all the child elements in my list now return false also as opposed to linking to the correct page...
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
$("ul.sf-menu li.i").click(function(e) {
e.preventDefault();
});
}
Has anybody an idea on an alternative method that could work?
HTML
<ul class="sf-menu"> <li>Home<li class="i">Weddings
<ul>
<li>Peel Suite</li>
<li>The Hall</li>
<li>The Grounds</li>
<li>Food & Drink</li>
<li>Pricing</li>
</ul>
</li>
well. without HTML its kind of hard to tell. But you are stopping the default behavior of the browser when the user clicks on the li (and therefor also its children, which I suppose is an anchor/link).
you could check if its a link or an anchor on click and handle it differently;
$("ul.sf-menu li.i").click(function(e) {
if (e.target.nodeName!=="A"){
e.preventDefault();
//do your hover code
}
else{
//do nothing, because the user wants the link to load
}
});
change youre selector so it only matches the first level of listpoints, also i dont see a class i so you might need to drop that from the selector aswell.
$("ul.sf-menu > li")
I have using the following code for developing tabs.
$(document).ready(function() {
$('#container-1').tabs();
}
....
<div id="container-1">
<ul>
<li><span>Home</span></li>
<li><span>Contact</span></li>
</ul>
</div>
...
It works fine! I need the tab click event. If it is the Home tab click, I have to do alert();. How do I achieve this?
Set the id of the Home tab span element:
<li><span id="home">Home</span></li>
And add the click handler to it somewhere:
$("#home").click(function()
{
alert("Home tab is selected!");
});
Personally, I'd handle it all in the tab configuration itself rather than adding click events to the elements which ultimately will be the clickable part of the tab. If you do it via the tab config, then all of your tab logic is centralized thus making things cleaner and you don't need to be familiar with the implementation details of the tabs:
$(document).ready(function() {
$('#container-1').tabs({
selected : function(e, ui) {
if (ui.index == 0) {
alert('Home clicked!');
}
}
});
});
....
<div id="container-1">
<ul>
<li><span>Home</span></li>
<li><span>Contact</span></li>
</ul>
</div>
If you want the tab click event, you would do something like one of the following.
$("#tabid").click(function(e) {
e.preventDefault();
// Do tab click logic
});
or
$(".tabclass").click(function(e) {
e.preventDefault();
// Do tab click logic
});
Do a search for jQuery cheat sheet to get yourself a very useful jQuery cheat sheet.