I need to make a function, which show menu, if user click on specific div. Then I need to hide it, if user click anywhere on website, except that one specific div.
For example
<div class="showMeHideMe" style="display:none;">Example</div>
<div class="showIt">Show it!</div>
And then my JavaScript
jQuery('.showIt').click(function(){
$('.showMeHideMe').show();
});
jQuery(document).not('.showMeHideMe').click(function(){
$('.showMeHideMe').hide();
});
If i click on .showIt, everyting working fine and .showMeHideMe is showing. Then if I click anywhere on page, .showMeHideMe is hiding. Also OK. But if it's shown and I click on .showMeHideMe, it hides itself. Which is wrong.
Can you please tell me what I'm doing wrong?
UPDATE
Thank you very much for very fast replies.
I guess it will be better to show it exactly on the page that I need to fix. Now my code looks like this :
<div class="search--box">
<div class="search--box-inner">
<form role="search" method="get" id ="searchform" action="">
<input class="search--box--input" name="s">
</input>
</form>
</div>
</div>
And JavaScript which should handle this
jQuery(".search--box").hide();
jQuery(".desktop-search").mouseenter(function (e) {
e.stopPropagation();
jQuery(".search--box").show();
jQuery(".search--box--input").focus();
});
jQuery(document).not('input.search--box--input').click(function (e) {
e.stopPropagation();
jQuery(".search--box").hide();
});
Also the variant with :not, instead of .not() is not working. Still if I click on the input, it's still hiding.
Finally we handle it with condition, that check if clicked element have specific class.
Maybe it could be usable for someone who have the same issue as me :)