When click on menu, it opens, when click again it close
Just use fadeToggle()
:
$(function () { $(".showMenu").click(function () { $(this).next(".menu").fadeToggle(400); });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><button class="showMenu">Show Menu</button><div class="menu" style="display: none;"> <ul> <li>Menu 1</li> <li>Menu 2</li> <li>Menu 3</li> </ul></div>
When i click on menu button it open menu but when i click close button its not closing it
Every time you click on the container #MENU
, it will show the menu. The problem is that even when the menu is open, clicking on #MENU
will show the menu. So clicking the "close" button will close the menu, then open it again.
One way you could fix this is by storing the state of the menu in a variable - like this:
var OPEN = false,
MENU_ELEM = document.getElementById("MENU");
function TOGGLEMENU(){
if(OPEN){
//menu is open, we need to close it
OPEN = false;
MENU_ELEM.innerHTML = "<p onclick='TOGGLEMENU()'>MENU</p>";
} else {
//menu is closed, we need to open it
OPEN = true;
MENU_ELEM.innerHTML = "<a style='text-decoration:none;' href='OUR_WORKS.html'>OUR_WORKS</a><p class='LGND'>ABCD</p><button id='CLOSE'class='LGND' onclick='TOGGLEMENU()'>CLOSE_MENU</button>";
}
}
<div id="MENU"><p onclick="TOGGLEMENU()">Menu</p></div>
Jquery - trigger submenu on click then close on second click
[RESOLVED] I asked a colleague on Facebook and here is the code that worked :
$('.menu-item-has-children > a').click(function() {
//$('.menu-item-has-children > li').not(this).find('ul').slideUp();
var $submenu = $(this).next('.sub-menu');
$('.sub-menu').not($submenu).slideUp(200); // fermer les autres menus
$submenu.slideToggle(200); // slideToggle permet d'ouvrir ou fermer, selon l'état
});
.sub-menu {
display:none
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav id="primary-nav">
<li class="menu-item menu-item-has-children">
<a href="#">Parent menu</a>
<ul class="sub-menu">
<li class="menu-item"><a title="Contract Negotiation">Contract Negotiation</a></li>
<li class="menu-item"><a title="Contract Negotiation">Contract Negotiation</a></li>
<li class="menu-item"><a title="Contract Negotiation">Contract Negotiation</a></li>
</ul>
</li>
<li class="menu-item menu-item-has-children">
<a href="#">Parent menu</a>
<ul class="sub-menu">
<li class="menu-item"><a title="Contract Negotiation">Contract Negotiation</a></li>
<li class="menu-item"><a title="Contract Negotiation">Contract Negotiation</a></li>
<li class="menu-item"><a title="Contract Negotiation">Contract Negotiation</a></li>
</ul>
</li>
</nav>
Close the previous dropdown menu when click on the next dropdown menu opener button and keep that menu opened
Replace the eventlistener with this:
window.addEventListener('click', function(event) {
var editMenuDropdowns = document.getElementsByClassName('edit-menu-drop-down-box');
for (var j = 0; j < editMenuDropdowns.length; j++) {
var openEditMenuDropdown = editMenuDropdowns[j];
if (openEditMenuDropdown.classList.contains('show-edit-menu-drop-down-box') && event.target.closest('.edit-menu-icon-con') !== openEditMenuDropdown.parentElement) {
openEditMenuDropdown.classList.remove('show-edit-menu-drop-down-box');
}
}
});
All we're doing here is telling the "editMenuDropdowns" loop to not close the closest menu when clicked, but it'll close all other menus.
And Dennis is correct in that the eventlistener function is inside the function above it when it should be on its own.
How to close previous opened sub menu when clicked on next submenu on click event
Use slideUp()
and slideDown()
like below:-
$('.sub-menu').hide();$("li:has(ul)").click(function(){ $(".main-list").find('.sub-menu').slideUp(); $(this).find('.sub-menu').slideDown();});
.menu ul.nav-menu{ list-style: none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="menu"> <ul class="nav-menu"> <li class="main-list"><a href="#">home</a> <ul class="sub-menu"> <li><a href="#">home1</a></li> <li><a href="#">home2</a></li> </ul> </li> <li class="main-list"><a href="#">service</a> <div class="square"> <div class="sub"> <ul class="sub-menu"> <li><a href="#">home1</a></li> <li><a href="#">home2</a></li> </ul> </div> </div> </li> </ul></div>
How to close the drop down menu again if clicked on the same element?
In the below snipet I after clicking on span , I used [.find()][1]
, to find the i
tag and check whether it has a certain class , using [.hasClass()][1]
then replace the fa-plus
with fa-minus
or make the inverse then show the sibling dropdown .
This should help you :
$(document).ready(function() { "use strict"; $(".dropdown").hide(); $("#smpg-cat-list li span").click(function() { $(".dropdown").hide(); var $i = $(this).find("i"); console.log($i); if ($i.hasClass("fa-plus")) { $i.removeClass("fa-plus").addClass("fa-minus"); $(this).next('.dropdown').show(); } else { $i.removeClass("fa-minus").addClass("fa-plus"); $(this).next('.dropdown').hide(); } });});
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ul id="smpg-cat-list"> <li><a href="#">Web Design</a> </li> <li><a href="#">Robots</a></li> <li> <a href="#">Programming</a> <span class="toggle" style="cursor:pointer"><i class="fa fa-plus" aria-hidden="true"></i></span> <ul class="dropdown"> <li><a href="#">PHP</a></li> <li><a href="#">C#</a></li> </ul> </li> <li> <a href="#">Frameworks</a> <span class="toggle" style="cursor:pointer"><i class="fa fa-plus" aria-hidden="true"></i></span> <ul class="dropdown"> <li><a href="#">Laravel</a></li> </ul> </li> <li><a href="#">Data Analysis</a></li> <li> <a href="#">CMS</a> <span class="toggle" style="cursor:pointer"><i class="fa fa-plus" aria-hidden="true"></i></span> <ul class="dropdown"> <li><a href="#">WordPress</a></li> <li><a href="#">Joomla</a></li> </ul> </li></ul>
How to make dropdown close or open when I click on the button when there is ref used? (React)
It will be triggering both functions at the same time. Which causes problems. You Have to Include the Hamburger button also in the ref.
const Navbar = () => {
const [open, setOpen] = useState(false);
const ref = useRef();
useEffect(() => {
const handleClickOutside = (event) => {
if (!ref?.current?.contains(event.target)) {
setOpen(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [ref]);
return (
<div className="navbar_container">
<div className="navbar_inner_container">
<div className="logo_container">
<img src={Logo} alt="logo" className="navbar_logo" />
</div>
<section ref={ref}>
<div
className="hamburger_menu"
onClick={() => {
setOpen(!open);
}}
>
<Dropdown open={open} />
</div>
</section>
</div>
</div>
);
};
export default Navbar;
const Dropdown = ({ open }) => {
return (
<div >
{open === true ? (
<div className="dropdown_container">
<div
className="dropdown_item"
onClick={() =>
window.scrollTo({
top: document.getElementsByClassName("functions_container")[0]
.offsetTop,
behavior: "smooth",
})
}
>
<button className="navbar_link">Functions</button>
</div>
<div
className="dropdown_item"
onClick={() =>
window.scrollTo({
top: document.getElementsByClassName("partners_container")[0]
.offsetTop,
behavior: "smooth",
})
}
>
<button className="navbar_link">Partner</button>
</div>
<div
className="dropdown_item"
onClick={() =>
window.scrollTo({
top: document.getElementsByClassName("statements_container")[0]
.offsetTop,
behavior: "smooth",
})
}
>
<button className="navbar_link">Statements</button>
</div>
<div
className="dropdown_item"
onClick={() =>
window.scrollTo({
top: document.getElementsByClassName("contact_container")[0]
.offsetTop,
behavior: "smooth",
})
}
>
<button className="navbar_link">Contact</button>
</div>
<img
src={LanguageFlag}
alt="lanugage_flag"
className="language_flag"
/>
</div>
) : null}
</div>
);
};
export default Dropdown;
Related Topics
Want to Hide Menu by Clicking Outside the Menu Element
Navigationduplicated Navigating to Current Location ("/Search") Is Not Allowed
Focus Next Input Once Reaching Maxlength Value
How to Create a Horizontal Scrolling Chart.Js Line Chart With a Locked Y Axis
Passing Multiple Parameters to Function Using Onclick
Map, and Removing Commas from an Array in JavaScript
How to Get the Latest and Oldest Record in Mongoose.Js (Or Just the Timespan Between Them)
React Use State to Show Otherwise Hide
How to Format a Number to 2 Decimal Places, But Only If There Are Already Decimals
Calculate the Bounding Box'S X, Y, Height and Width of a Rotated Element Via JavaScript
Change Image on Scroll Position
Converting Excel Date Serial Number to Date Using JavaScript
Why Am I Not Able to Add Input Text into My Table
How to Tell If a Dom Element Is Visible in the Current Viewport
Exclude a Folder from Glob Pattern Matching
How to Check Whether Dynamically Attached Event Listener Exists or Not
How to Call the Same Method for Different Elements With Different Ids
Resource Blocked Due to Mime Type Mismatch (X-Content-Type-Options: Nosniff)