4

I tried Chinnu R's approach, but when I click anywhere outside the div, menu items not hiding, only when I click inside the div, menu items hide, I want the opposite, ie, click outside div, hide, click inside div, stay put.

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

<script>
$(document).ready(function() {
        $("button").click(function() {
            $('div#nomore').toggle();
        });


        $("body > *").not("body > button").click(function(e) {
            $('div#nomore').hide();
        });
    });

</script>

</head>
<body>
<button>MENU</button>
<div id="nomore" style="display:none; background-color:yellow; width:300px;">
<br>
<a href="www.yahoo.com">Yahoo</a><br>
ITEM 2<br>
ITEM 3<br>
ITEM 4<br>
ITEM 5<br>
</div>
</body>
</html>
Cindy Turlington
  • 2,456
  • 3
  • 15
  • 20
  • check this jsfiddle,i think rhis will solve your problem. [1]: http://jsfiddle.net/jc7xA/1/ – Sanjay Nov 07 '13 at 04:57

9 Answers9

4

Can you try this,

        $(document).ready(function() {
            $("button").click(function() {
                $('div#nomore').toggle();
            });

            $("body").click(function(e) {
                 if(e.target.id == "nomore"){
                     $('div#nomore').hide();
                 }
            });
        });

Other method:

        $(document).ready(function() {
            $("button").click(function() {
                $('div#nomore').toggle();
            });         


            $("body > *").not("body > button").click(function(e) {
                $('div#nomore').hide();
            });
        });

Updated code:

            $("body > *").not("body > button").click(function(e) {
                 console.log(e.target.id);
                if(e.target.id=='nomore'){
                        return false;
                 }
                 $('div#nomore').hide();

            });
Krish R
  • 22,583
  • 7
  • 50
  • 59
3

Use:

$("button").click(function (e) {
    e.stopPropagation();
    $('div#nomore').toggle();
});
$('body').click(function () {
    $('div#nomore').hide();
});

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
1
$(document).mouseup(function (e)
{
        $('div#nomore').hide();
});
Neel
  • 11,625
  • 3
  • 43
  • 61
1

stopPropagation on the events for button and div#nomore and bind a click to window.

That way when window is click anywhere but button or div#nomore, it will hide the menu.

$(document).ready(function(){
    $("button").click(function(e){
        $('div#nomore').show();
        e.stopPropagation();
    });
    $('div#nomore').click(function(e){
        e.stopPropagation();
    });
    $(window).click(function(){
        $('div#nomore').hide();
    });
});

And if you want the menu to disappear when you click on one of the menu items, remove the $('div#nomore').click ... block.

JSFiddle example.

Robbie Wxyz
  • 7,671
  • 2
  • 32
  • 47
1

Try this,

$('*:not(div#nomore)').on('click',function(e){
    e.stopPropagation();
    if(e.currentTarget!='button' &&  $('div#nomore').is(':visible'))
    {
        $('div#nomore').hide();
    }
    else
    {
        $('div#nomore').toggle();
    }
});

Demo

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
1

replace the div as shown below`

<div id="nomore" tabindex="-1" onblur="$('div#nomore').toggle();"
raghu
  • 59
  • 1
  • 9
1

after extensive research, I have determined that it is impossible to make this work on all platforms: desktop, android and iphones, need to adopt a different approach...

Cindy Turlington
  • 2,456
  • 3
  • 15
  • 20
0

Try this....

$(document).mouseup(function (e)
{
    var container = $("#yourDiv Id");

    if (!container.is(e.target) // if the target of the click isn't the div...
        && container.has(e.target).length === 0) // ... nor a descendant of the div
    {
        container.hide();
    }
});
Manwal
  • 23,450
  • 12
  • 63
  • 93
0
$('body :not(div#nomore)').on('click',function(){
 $('div#nomore').hide();
})
Ankit Tyagi
  • 2,381
  • 10
  • 19