Jquery Click function excluding child elements
on May.12, 2011, under Technology
I’ve been searching for a way to make a click function work
only on a certain element, excluding the element’s children..
After a short search, I’ve found this function
[js]
$(".example").click(function(){
$(this).fadeOut("fast");
}).children().click(function(e) {
return false;
});
[/js]
However it was disturbing some click functions I made for the child
elements, so, I built something a little better:
[js]
$(".example").click(function(data, handler){
if (data.target == this) {
//Do Stuff (only element clicked, not children)
}
});
[/js]
Not very complex.
December 14th, 2011 on 1:41 pm
Simple and good! Thx!
January 17th, 2012 on 7:16 am
Excellent. Exactly what I was looking for. Thanks.
February 29th, 2012 on 1:54 am
Thank you, this is exactly what I was looking for and it works perfectly.
March 20th, 2012 on 8:36 am
Brilliant! Such a simple and effective code. Thanks