INeedHelp INeedHelp - 4 years ago 66
HTML Question

How can I get my button to work with the rest of my javascript?

I am attempting to make a button that makes drop down menu pop up. I want the user to be able to toggle the pop up menu off by either pressing the button again or by clicking outside of the menu. With my current code I can use the button to open the drop down menu, but the button stops working afterwords. Exiting the menu by clicking outside of the menu works just fine. How can I get my button to stay working after I open the menu

Here is my code:

<!DOCTYPE html>
<html>
<head>
<style>
body{ margin:0px; background:#999; }
div#topbar > #sections_panel{
position:absolute;
height:0px;
width:550px;
background:#000;
top:60px;
left:0px;
border-radius:0px 0px 8px 8px;
overflow:hidden;
z-index:10000;
}
div#topbar > #sections_panel > div{
background:#333;
padding:20px;
height:238px;
margin:10px;
color:#FC0;
}
</style>
<script>
function toggleNavPanel(x){
var panel = document.getElementById(x), maxH="300px";
var box = document.getElementById('sections_panel')

if(panel.style.height == maxH){
panel.style.height = "0px";
} else {
panel.style.height = maxH;
}

window.addEventListener('mouseup', function(event){
if(event.target != box && event.target.parentNode !=box){
panel.style.height = "0px";

}
});
}


</script>
</head>
<body>
<div id="topbar">
<div id="logo">LOGO</div>
<div id="sections_btn_holder">
<button onclick="toggleNavPanel('sections_panel')">Navigator <span id="navarrow">&#9662;</span></button>
</div>
<div id="sections_panel">
<div>
Try adding things like more child div containers, links, buttons, menus, pictures, paragraphs, videos, etc... This area can display way more than just menu buttons or links. You will see a lot of modern sites even adding graphics, icons, animations and images to their drop down menus nowadays. So get creative partner.
</div>
</div>
</div>
</body>
</html>


New code for multiple buttons (for anyone interested in doing multiple buttons):

<!DOCTYPE html>
<html>
<head>
<style>
body{ margin:0px; background:#999; }
div#topbar > #sections_panel{
position:absolute;
height:0px;
width:550px;
background:#000;
top:200px;
left:0px;
border-radius:0px 0px 8px 8px;
overflow:hidden;
z-index:10000;
}
div#topbar > #sections_panel > div{
background:#333;
padding:20px;
height:238px;
margin:10px;
color:#FC0;
}

div#topbar1 > #sections_panel1{
position:absolute;
height:0px;
width:550px;
background:#000;
top:250px;
left:0px;
border-radius:0px 0px 8px 8px;
overflow:hidden;
z-index:10000;
}
div#topbar1 > #sections_panel1 > div{
background:#333;
padding:20px;
height:238px;
margin:10px;
color:#FC0;
}
</style>
<script>
function toggleNavPanel(dropDivId, height, btnId){
var panel = document.getElementById(dropDivId), maxH= height, nav = btnId;
if(panel.style.height == maxH){
panel.style.height = "0px";
} else {
panel.style.height = maxH;
}
window.addEventListener('mouseup', function(event){
if(event.target != panel && event.target.parentNode !=panel && event.target.id != nav ){
panel.style.height = "0px";
}
});
}
</script>


</head>
<body>
<div id="topbar">
<div id="logo">LOGO</div>
<div id="sections_btn_holder">
<button id="nav2" onclick="toggleNavPanel('sections_panel', '300px','nav2')">Navigator &#9662;</button>
</div>
<div id="sections_panel">
<div>
hahahahaha
</div>
</div>
</div>

<div id="topbar1">
<div id="logo1">LOGO</div>
<div id="sections_btn_holder1">
<button id="nav1" onclick="toggleNavPanel('sections_panel1', '300px','nav1')">Navigator &#9662;</button>
</div>
<div id="sections_panel1">
<div>
hahahahaha
</div>
</div>
</div>
</body>
</html>

Answer Source

Cause: When you click the button, both events are fired.

  • The MouseUp event handler is called first, closing the menu.
  • The OnClick event handler is called next, detecting that the menu is closed, consequently opening it again.

With today's browsers, this all happens without a glitch; seemingly nothing happened.

Solution: Add a condition to the MouseUp event handler:

if (event.target != box && event.target.parentNode !=box && event.target.tagName != "BUTTON"){

or

if (event.target != box && event.target.parentNode != box && event.target !== nav){

For the latter approach to work, you need to give the button an ID:

<button id="nav" onclick="toggleNavPanel('sections_panel')">Navigator <span id="navarrow">&#9662;</span></button>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download