Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$('html').click()... anywhere except one element

I have a dynamically appended menu which I am removing if you click anywhere on page including the menu links itself. What I am trying to achieve is to prevent the remove if you click a specific link and that simply does not work for me. Unfortunately I cant use the delegate method, if that would help, due to old version on jquery used on client side, no option to update it.

So maybe you could suggest if there is any way to do so. Here is a quick example of mine.

<script>     $(function() {         $('.menu').append('<a href="" class="solid">Option</a> <a href="">Option</a> <a href="">Option</a>');                                     $('.menu a').live('click',function(){             return false;         });                   $('a.solid').live('click',function(){             return false;         });          $('html').click(function() {                                 $('.menu').remove();                         });                                                   }); </script> 

and the container

<div class="menu"></div> 
like image 793
devjs11 Avatar asked Apr 03 '12 11:04

devjs11


People also ask

How do I hide a div by clicking anywhere on the page?

$(document). click(function (event) { $('#myDIV:visible'). hide(); });

How do I detect a click outside an element?

To detect click outside element with JavaScript, we can use the element's contains method. const specifiedElement = document. getElementById("a"); document. addEventListener("click", (event) => { const isClickInside = specifiedElement.


1 Answers

Maybe it will work like this

$('html').click(function(e) {                        if(!$(e.target).hasClass('solid') )    {        $('.menu').remove();                    } });  

see: http://jsfiddle.net/fq86U/2/

like image 60
sneeky Avatar answered Sep 29 '22 22:09

sneeky