0

Here my code:

<div class="container" role="main">
    <li><a href="javascript:void(0);" onclick="showAssignLabelsInMain(this);">Assign Labels</a></li>
    <div class="labelListInMain" id="labelListInMain">
        <g:render template=""/>
    </div>
</div>

function showAssignLabelsInMain(elm) {
        $.ajax({type: 'POST',
            url: '${createLink(controller: "HRAdmin", action: "labelListInMain")}',
            success: function (data, textStatus) {
                $('#labelListInMain').html(data);
            }
        });
        var offset = $(elm).offset();
        $("#labelListInMain").css({top: offset.top + 25, left: offset.left - 18, position: 'absolute'});
        $('#labelListInMain').toggle('fast');

$(".container").mouseup(function (e) {
        var subject = $("#labelListInMain");
        alert(e.target.id)

        if (e.target.id != subject.attr('id')) {
            subject.fadeOut();
        }
    });

It means that when I clicked the button, toggle will show up, but when I click inside that toggle, it fades out. It is not supposed to fade out and I don't know what's wrong.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Tran Tam
  • 699
  • 3
  • 14
  • 27

3 Answers3

1
$(".link").click(function(e){
e.preventDefault();
$(".popup").fadeIn(300,function(){$(this).focus();});
});

$('.close').click(function() {
 $(".popup").fadeOut(300);
});
$(".popup").on('blur',function(){
 $(this).fadeOut(300);
});

Demo

Hope it works for you

Neeraj Dubey
  • 4,401
  • 8
  • 30
  • 49
  • Thanks guys +1 for demo! – Tran Tam Jul 31 '14 at 06:15
  • Excuse me bro, Can I ask a question? $(".popup").on('blur',function(){ means that when pop u opened, click mouse inside that, blur even will not happened right? Cause I tried it on my project and when I clicked in pop up, it faded out. – Tran Tam Aug 01 '14 at 04:01
  • Please share your HTML or create Fiddle demo. – Neeraj Dubey Aug 01 '14 at 05:45
  • Here my code [link](http://jsfiddle.net/5xVg9/), I applied your code but it's not work on my project. – Tran Tam Aug 01 '14 at 08:18
  • Hey i have checked you Demo but it contains only your javascript part not HTML and javascript code is not mine it your which is asked in question. – Neeraj Dubey Aug 01 '14 at 13:14
0

You can also do from another method

$(".container").click(function (e) {
    var subject = $("#labelListInMain");

    if (!$(e.target).closest('.labelListInMain').length) {
        subject.fadeOut();
    }
});
Neeraj Dubey
  • 4,401
  • 8
  • 30
  • 49
0

Your question seems to be connected to something like this

How to have click event ONLY fire on parent DIV, not children?

you want the div to fade when the .container is clicked but not if it was toggle that was clicked right? and toggle is a child of .container try looking this up too http://api.jquery.com/children/

Community
  • 1
  • 1
Miu
  • 55
  • 10