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.
Adebayo OjoRegional Network ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Determine the Perfect Price for Your IT Services

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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.