Question

I have following HTML markup.

<div id="toppanel">
    <div id="panel" style="display: none;">
        <div class="content clearfix">
            <div class="clear"></div>
            //some ul li
            <div class="left">
                <img alt="" src="img/slide_logo.png">
                <h1>Welcome</h1>
                <p class="grey">You can put request for anything you want : cooking, driver for a day, programmer... The only limit is your imagination!</p>                    
            </div>
            <div class="left">
                <h1>Member Login</h1>
                <form accept-charset="utf-8" action="/users/login" method="post" id="HeaderUserLoginForm" novalidate="novalidate">
                    //form fields
                    <input type="submit" style="float:left;position: relative;top: 10px;" class="button blue" value="Sign In" name="submit">
                    <a href="#" class="lost-pwd">Lost your password?</a>
                </form>
            </div>

            <div class="left right">
                <form accept-charset="utf-8" action="/users/signup" method="post" id="HeaderUserSignupForm" novalidate="novalidate">
                    //form fields
                    <input type="submit" style="float:left;position: relative;top: 10px;" class="button blue" value="Sign Up" name="submit">                            
                </form>
            </div>
        </div>
    </div>
</div>

Above HTML code is for top fixed login and sign up which is slideup and down using toggle.

Now when user click on other part of DOCUMENT i need to hide this and i did it with below code which is working.

$(document).on('click',function(e)
{
    e.stopPropagation();
    $("div#panel").slideUp("slow");
});

But problem is that when i click on any part of toppanel DOM then it also slideUp.

So how can i overcome this problem. I have also tried :not() but with no hope.

what i end up is.

$(document).on('click','#panel input[type!="submit"], #toppanel:not("div"), #toppanel:not("div ul"), #toppanel:not("div p")',function(e)
{
    e.stopPropagation();
    $("div#panel").slideUp("slow");
});

which is working just for "#panel input[type!="submit"]" not for other.

Thanks So Much.

Was it helpful?

Solution

$(document).on('click',function(e)
{
     if(!$(e.target).is('#toppanel')){
       e.stopPropagation();
       $("div#panel").slideUp("slow");
    }

});

update..(not ta good way i think..anyway give a try) give all the elements a class that you want to exclude the click event

<div class="exclude">
   <p class="exclude"></p>
    <span class="exclude">fooo</span>
    etc....

</div>



 $(document).on('click',function(e)
    {
         if(!$(e.target).is('.exclude')){
           e.stopPropagation();
           $("div#panel").slideUp("slow");
        }

    });

OTHER TIPS

Well ubercooluk thanx for help this is what i endup with and its working.

$(document).on('click',function(e)
{
    if($(e.target).parents('#toppanel').length == 0 )
    {
        e.stopPropagation();
        $("#toggle a.close").hide();
        $("#toggle a.open").show();
        $("div#panel").slideUp("slow");
    }
});

may be this could help:

$('#panel').children().on('click', function(e){
    e.stopPropagation();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top