I adapted the example found here to be a bit more like the “real” menu in which it should crash on user clicks.
I like the fact that all the visual aspects of the menu are considered in css, with javascript entering the game only to handle the action required when the user selects a parameter. I believe this is the most suitable approach for my specific application.
However, I find the onclick event that I assign for each link is removed (or ignored) when the menu is removed.
I publish 2 versions that work in the sense that when users select a parameter, the desired onclick event is fired, and the other one beautifully discards the menu and also swallows the onclick event. I get a solution that combines 2 results into a work menu, which will also satisfy the requirement to postpone itself after a click.
EDIT just noticed some additional code that I commented on (an earlier attempt before computing only the css solution. I called mouseleave without effect.
The only difference between the two versions is the addition of the CSS clause:
#menuwrapper ul:active{
display:none;
}
A version that "works" but does not collapse the menu
var display = document.getElementById("display");
$(document).ready(function() {
$('li.menu_clickable').click(function() {
if (this.children.length === 1) {
display.innerHTML = 'you clicked ' + this.innerHTML;
}
});
});
body {
font-family: Arial;
font-size: 12px;
}
#maindiv {
position: absolute;
left: 200px;
top: 0px;
}
#menuwrapper ul,
#menuwrapper ul li {
margin: 0;
padding: 0;
list-style: none;
}
#menuwrapper ul li {
background-color: #7f95db;
border-bottom: solid 1px white;
width: 150px;
cursor: pointer;
}
#menuwrapper ul li:hover {
background-color: #6679e9;
position: relative;
z-index: 100;
}
#menuwrapper ul li a {
padding: 5px 15px;
color: #ffffff;
display: inline-block;
text-decoration: none;
}
#menuwrapper ul li ul {
position: absolute;
display: none;
}
#menuwrapper ul li:hover ul {
left: 150px;
top: 0px;
display: block;
}
#menuwrapper ul li ul li {
background-color: #cae25a;
}
#menuwrapper ul li:hover ul li:hover {
background-color: #b1b536;
}
#menuwrapper ul li ul li a {
color: #454444;
display: inline-block;
width: 120px;
}
#menuwrapper ul li:hover ul li ul {
position: absolute;
display: none;
}
#menuwrapper ul li:hover ul li:hover ul {
display: block;
left: 150px;
top: 0;
}
#menuwrapper ul li:hover ul li:hover ul li {
background: #86d3fa;
}
#menuwrapper ul li:hover ul li:hover ul li:hover {
background: #358ebc;
}
#menuwrapper ul li:hover ul li:hover ul li a {
color: #ffffff;
}
.clear {
clear: both;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menuwrapper">
<ul>
<li class="menu_clickable"><a href="#">Home</a>
</li>
<li class="menu_clickable"><a href="#">Products</a>
<ul>
<li class="menu_clickable"><a href="#">Product 1</a>
<ul>
<li class="menu_clickable"><a href="#">Sub Product 1</a>
</li>
<li class="menu_clickable"><a href="#">Sub Product 2</a>
</li>
<li class="menu_clickable"><a href="#">Sub Product 3</a>
</li>
</ul>
</li>
<li class="menu_clickable"><a href="#">Product 2</a>
</li>
<li class="menu_clickable"><a href="#">Product 3</a>
</li>
</ul>
</li>
<li><a href="#">About Us</a>
<ul>
<li class="menu_clickable"><a href="#">Faqs</a>
</li>
<li class="menu_clickable"><a href="#">Contact Us</a>
</li>
<li class="menu_clickable"><a href="#">Where are we?</a>
</li>
</ul>
</li>
<li class="menu_clickable"><a href="#">Help</a>
</ul>
</div>
<div id="maindiv">
<div>
<label id="display">click an option</label>
</div>
<div>
<button onclick="alert('hey!');">
some button
</button>under the menu
</div>
<div>
<input value="some input box">under the menu
</div>
</div>
Run codeHide resultwhich destroys the menu but does not call onclick
var display = document.getElementById("display");
$(document).ready(function() {
$('li.menu_clickable').click(function() {
if (this.children.length === 1) {
display.innerHTML = 'you clicked ' + this.innerHTML;
}
});
});
body {
font-family: Arial;
font-size: 12px;
}
#maindiv {
position: absolute;
left: 200px;
top: 0px;
}
#menuwrapper ul,
#menuwrapper ul li {
margin: 0;
padding: 0;
list-style: none;
}
#menuwrapper ul li {
background-color: #7f95db;
border-bottom: solid 1px white;
width: 150px;
cursor: pointer;
}
#menuwrapper ul li:hover {
background-color: #6679e9;
position: relative;
z-index: 100;
}
#menuwrapper ul li a {
padding: 5px 15px;
color: #ffffff;
display: inline-block;
text-decoration: none;
}
#menuwrapper ul li ul {
position: absolute;
display: none;
}
#menuwrapper ul li:hover ul {
left: 150px;
top: 0px;
display: block;
}
#menuwrapper ul li ul li {
background-color: #cae25a;
}
#menuwrapper ul li:hover ul li:hover {
background-color: #b1b536;
}
#menuwrapper ul li ul li a {
color: #454444;
display: inline-block;
width: 120px;
}
#menuwrapper ul li:hover ul li ul {
position: absolute;
display: none;
}
#menuwrapper ul li:hover ul li:hover ul {
display: block;
left: 150px;
top: 0;
}
#menuwrapper ul li:hover ul li:hover ul li {
background: #86d3fa;
}
#menuwrapper ul li:hover ul li:hover ul li:hover {
background: #358ebc;
}
#menuwrapper ul li:hover ul li:hover ul li a {
color: #ffffff;
}
#menuwrapper ul:active{
display:none;
}
.clear {
clear: both;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menuwrapper">
<ul>
<li class="menu_clickable"><a href="#">Home</a>
</li>
<li class="menu_clickable"><a href="#">Products</a>
<ul>
<li class="menu_clickable"><a href="#">Product 1</a>
<ul>
<li class="menu_clickable"><a href="#">Sub Product 1</a>
</li>
<li class="menu_clickable"><a href="#">Sub Product 2</a>
</li>
<li class="menu_clickable"><a href="#">Sub Product 3</a>
</li>
</ul>
</li>
<li class="menu_clickable"><a href="#">Product 2</a>
</li>
<li class="menu_clickable"><a href="#">Product 3</a>
</li>
</ul>
</li>
<li><a href="#">About Us</a>
<ul>
<li class="menu_clickable"><a href="#">Faqs</a>
</li>
<li class="menu_clickable"><a href="#">Contact Us</a>
</li>
<li class="menu_clickable"><a href="#">Where are we?</a>
</li>
</ul>
</li>
<li class="menu_clickable"><a href="#">Help</a>
</ul>
</div>
<div id="maindiv">
<div>
<label id="display">click an option</label>
</div>
<div>
<button onclick="alert('hey!');">
some button
</button>under the menu
</div>
<div>
<input value="some input box">under the menu
</div>
</div>
Run codeHide result