Closing drop down tab by clicking outside.

Adebayo Ojo
Adebayo Ojo used Ask the Experts™
on
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
<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>

Open in new window


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*********************************//

Open in new window


I tried to add another jquery to hide the dropdown on clicking the tab icon(
img id="note_still" src="images/note_still.png"

Open in new window

but the dropdown refused to even open up when I applied the script.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
A couple of issues

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
//**********Function for notification drop-down menu*******//
// SAVE REFERENCE TO OPEN TAB
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');

    // EXCLUDE CLICKS ON OPEN TAB
    if (event.target != box && event.target.parentNode != box && event.target != openLink) {
        box.style.display = 'none';
		event.stopPropagation();
    }
});
//******

Open in new window

Adebayo OjoRegional Network Manager

Author

Commented:
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)
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Here is my implementation
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>

Open in new window

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>

Open in new window

Working sample here
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Adebayo OjoRegional Network Manager

Author

Commented:
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>

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
I don't understand - you took my solution made one change and made it your own and then accept your own comment as the solution.

BTW there was absolutely no reason to move line 3 to line12 - it was placed on line 3 for a very good reason.

document.getElementById('note_still'); is an invariant - that element remains unchanged - it is not efficient to go and retrieve that value every time the event fires.
Adebayo OjoRegional Network Manager

Author

Commented:
That modification actually worked for me.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial