• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 139
  • Last Modified:

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
<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.
0
Adebayo Ojo
Asked:
Adebayo Ojo
  • 4
  • 3
1 Solution
 
Julian HansenCommented:
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

0
 
Adebayo OjoRegional Network ManagerAuthor 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)
0
 
Julian HansenCommented:
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
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Adebayo OjoRegional Network ManagerAuthor 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

0
 
Julian HansenCommented:
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.
0
 
Adebayo OjoRegional Network ManagerAuthor Commented:
That modification actually worked for me.
0
 
Julian HansenCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now