Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select only the current element when using on click

Tags:

jquery

Is it possible to cleanly select only the current clicked div and not children when using .on("click",?

Basically I have a outer div that when faded in is 100% width and 100% height, it then has a container in the middle, my aim is to have it when you click the outer div it fades both out but not when you click on the inner div.

I've created a jFiddle here to demonstrate the issue: http://jsfiddle.net/silver89/kaxsL/

I've tried using div > #box but had no luck so looking for more suggestions?

like image 929
Dan Avatar asked Feb 01 '26 07:02

Dan


1 Answers

Try this:

$(document).on("click", "a", function(){
     $("#box").fadeIn("fast");
     return false;
});

$('div#box').on('click',function(e){
    $(this).fadeOut();
});

$('div.inner').on('click',function(e){
   e.stopPropagation();
});

You just need to stop the event from bubbling up. You can do whatever you want to select either div. ​

like image 144
bokonic Avatar answered Feb 04 '26 01:02

bokonic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!