I have a strange issue where my jquery menu comes up just fine the first time, but if the menu was closed by clicking outside the menu, it won’t come up again until the browser is refreshed. Please help.
The following will show the menu only the first time until page is refreshed:
document.getElementById("selectCourse").style.display = "block";
document.getElementById("selectCourse").style.display = "none";
The following will show the menu the first time, then will show subsequently for a brief moment then disappear:
$("#selectCourse").show("fast");
$("#selectCourse").hide("fast");
function wSelect() {
var name = "#selectCourse";
var menuYloc = null;
//document.getElementById("selectCourse").style.display = "block"; // Display menu
$("#selectCourse").show("fast");
$(document).ready(function() {
menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
$(window).scroll(function () {
offset = menuYloc+$(document).scrollTop()+"px";
$(name).animate({top:offset},{duration:500,queue:false});
});
$("html").click(function() {
//document.getElementById("selectCourse").style.display = "none";
$("#selectCourse").hide("fast");
});
});
}
<div id="selectCourse">
<div id="floatMenu">
<p>Select a Course</p>
<ul id="choices" class="menu">
<li>
<a href="course1.html" onlick="return true;">
<img src="course1.png" height="40">Course 1</a>
</li>
<li>
<a href="course2.html" onlick="return true;">
<img src="course2.png" height="40">Course 2</a>
</li>
</ul>
</div>
</div>
#selectCourse {
display: none;
width: 300px;
}
#floatMenu {
position: absolute;
background-color: #0078d0;
border: 1px solid #000;
top: 80px;
right: 10%;
margin: 5px;
}
#floatMenu ul {
margin: 20px 30px 20px 0;
font-size: 16px;
list-style-type: none;
}
#floatMenu ul li a {
display: flex;
align-items: center;
text-decoration: none;
color: #ccc;
padding: 5px 10px;
}
#floatMenu ul li a:hover {
color:#fff;
background-color:#333333;
}
/*
#floatMenu ul.menu li a:hover {
border-left: 4px solid #f09;
}
*/
#floatMenu p {
color: #ffffff;
text-align: left;
margin-left: 40px;
font-size: 18px;
}
2
Answers
After fiddling with it for couple of days without a fix, I've moved on to JQuery UI Menu Widget that took care of the issue: https://api.jqueryui.com/1.10/menu/#:~:text=The%20menu%20widget%20uses%20the%20jQuery%20UI%20CSS,icons.%20ui-menu-item%3A%20The%20container%20for%20individual%20menu%20items.
your code is something confuse, but as per your requirement submenu should close when outside click.
to achieve this task you have to use,