Adebayo Ojo
asked on
Closing drop down tab by clicking outside.
Please I need someone to help me with this. I've tried to fix it but not working as expected. I want to close a drop down tab by clicking both the tab button and outside the tab. I was able to open the drop down by clicking on the tab button and close it by clicking outside the tab, but I cannot close the tab by clicking on the tab button. Below are my codes:
HTML
JavaScript/JQuery
I tried to add another jquery to hide the dropdown on clicking the tab icon(
HTML
<div id="notify" style="display:inline-block; cursor:pointer;">'.'<img id="note_still" src="images/note_still.png" width="31" height="30" alt="Notes">
<div id="notecontainer" class="notecontainer" style="display:none; border:solid 1px lightslategray; margin-top:2px;">';
<div id="notedisplay" class="notedisplay">'.$notify_result.'</div>';
<div id="spanBase" style="height:40px; width:300px; background-color:gray; text-align:center;"><a style="color:white; font-weight:800;" href="">View all notifications</a></div>
</div>
</div>
JavaScript/JQuery
//**********Function for notification drop-down menu*******//
$(document).ready(function() {
$("#notify").click(function() {
$("#notecontainer").show();
});
});
// Close the dropdown menu if the user clicks outside of it
window.addEventListener('mouseup', function(event) {
var box = document.getElementById('notecontainer');
if (event.target != box && event.target.parentNode != box) {
box.style.display = 'none';
}
});
//***************Ends Here*********************************//
I tried to add another jquery to hide the dropdown on clicking the tab icon(
img id="note_still" src="images/note_still.png"
but the dropdown refused to even open up when I applied the script.
ASKER
When I applied your code, I could open the tab and close it ONLY by clicking outside the dropdown, but cannot close it by clicking on the tab icon (#notify)
Here is my implementation
HTML
HTML
<div id="notify" style="display:inline-block; cursor:pointer;"><img id="note_still" src="images/note_still.png" width="31" height="30" alt="Notes">
<div id="notecontainer" class="notecontainer" style="display:none; border:solid 1px lightslategray; margin-top:2px;">
<div id="notedisplay" class="notedisplay">$notify_result</div>
<div id="spanBase" style="height:40px; width:300px; background-color:gray; text-align:center;"><a style="color:white; font-weight:800;" href="">View all notifications</a></div>
</div>
</div>
JavaScript<script>
//**********Function for notification drop-down menu*******//
var openLink = document.getElementById('note_still');
$(document).ready(function() {
$("#notify").click(function() {
$("#notecontainer").toggle();
});
});
// Close the dropdown menu if the user clicks outside of it
window.addEventListener('mouseup', function(event) {
var box = document.getElementById('notecontainer');
if (event.target != box && event.target.parentNode != box && event.target != openLink) {
box.style.display = 'none';
event.stopPropagation();
}
});
//***************Ends Here*********************************//
</script>
Working sample here
ASKER
Thanks Julian for the heads-up. I made just one modification in your JavaScript solution above and then it works. I moved code line 3 to line 12 as shown below:
<script>
//**********Function for notification drop-down menu*******//
$(document).ready(function() {
$("#notify").click(function() {
$("#notecontainer").toggle();
});
});
// Close the dropdown menu if the user clicks outside of it
window.addEventListener('mouseup', function(event) {
var box = document.getElementById('notecontainer');
var openLink = document.getElementById('note_still');
if (event.target != box && event.target.parentNode != box && event.target != openLink) {
box.style.display = 'none';
event.stopPropagation();
}
});
//***************Ends Here*********************************//
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That modification actually worked for me.
That modification actually worked for me.There is no reason it wouldn't work - I am just saying it was unnecessary.
As long as the var is in a scope where the event listener can access it - it will work - but keep it on line 12 if you wish - personally I am an efficiency nut I don't like code that does unnecessary actions - and getting the element on every single mouseup is unnecessary.
When you click the Open tab that is actually a click outside the box - so it triggers the Window.eventListener
When a click is recorded on the Tab it always does a show - so the box will never hide.
You have to do things
1. Make your click on the Open Tab toggle states on the dropdown
2. in the mouseup event handler you have to exclude clicks on the Open Tab - because otherwise the event handler will hide the box and the #notify click() eventhandler will toggle it on again
Open in new window