DNA
DNA

Reputation: 137

not jquery click function

I want to set display: none; on an id, but only if you click the id itself and not the img in that id

    $(":not('#lightbox img'),#lightbox").click(function() {
        $("#lightbox").css( "display", "none" );
    });

can someone tell me how te get this to work?

Upvotes: 0

Views: 34

Answers (2)

omikes
omikes

Reputation: 8513

Do it just like you wrote but change the first line:

$('#lightbox').find("*").add("#lightbox").click(function (event) {
    $(this).is('img') ? event.stopPropagation() : $("#lightbox").css("display","none");});

Upvotes: 0

AmmarCSE
AmmarCSE

Reputation: 30557

Use the target property of the Event object like

 $("#lightbox").click(function(e) {
        if(e.target.tagName != 'IMG'){
            $("#lightbox").css( "display", "none" );
        }
    });

Update

only if you click the id itself

Then, check the id of the target

$("#lightbox").click(function(e) {
            if(e.target.id == 'lightbox'){
                $("#lightbox").css( "display", "none" );
            }
        });

Upvotes: 3

Related Questions