1

Usually when I want to check if any child element inside a parent element was clicked I use something like this:

$(document).click(function (e) {
    alert($(e.target).is("#somediv *"));
}

But now I have the somediv in a variable. I have tried:

var somediv = $(this);
$(document).click(function (e) {
    alert($(e.target).is($(somediv, "*")));
}

But it doesn't work. How can I do that? Is it the best way to detect if a element or any child element was clicked?

2
  • It is an interesting question.. If you had a title as 'Check if an element belongs to a specific parent' or something like that Commented Sep 28, 2012 at 16:12
  • @Vega feel free to change it. I will approve it. Commented Sep 28, 2012 at 16:14

4 Answers 4

5

No matter whether somediv is a DOM element or a selector, use .closest [docs]:

if ($(e.target).closest(somediv).length > 0)

This will traverse the DOM up, starting at e.target and tests whether any ancestor (or the element itself) is somediv. If you want to exclude e.target, start at its parent:

$(e.target).parent().closest(somediv)

Another way, depending on the overall context, could be to simply bind the event handler to somediv:

somediv.click(function() {
   // click originated from inside the div
});

There is no easy way to create a selector from an existing DOM element. If somediv was containing a selector then you could use string concatenation to combine it:

$(e.target).is(somediv + ' *');    
Sign up to request clarification or add additional context in comments.

3 Comments

Does it work jsfiddle.net/GUAFW/1? Is the fiddle different from what is in OP?
FelixKling I tested and understood you code. It works. Nice one. I just need to close a div if any other element outside the div is clicked, so somediv.click it is not the solution.
Why haven't you asked this then? ;) There are already quite a few questions targeting this, for example: stackoverflow.com/questions/152975/….
1

Try below,

DEMO: http://jsfiddle.net/GUAFW/2/

$(function () {
    var somediv = $('#container');
    $(document).click(function (e) {
        alert($(e.target).parent().is(somediv));
        //                   ^__ Just immediate childrens :(
    });
});

You can do e.target != this inside the handler to check if it is the parent or from child element.

An Example:

HTML:

<div id="container">
   <div class="child"></div><div class="child">
</div>

JS:

$(function () {
    $('#container').click(function (e) {
        alert(e.target != this);
    });
});

6 Comments

I can't see how it applies because what I want to know is: any given click is happening inside a div or any of the div childs or not.
@lolol Yea, it was a misunderstanding from my side.. I realized that after looking at Felix answer.
@FelixKling May be saving the best for the last :P
I just feel left out, I want comments on my answer too :D
I'm getting there! Your awnser is different, I need to do some follow up to understand it. =p
|
0

Assuming someDiv has the string literal identifier of the element, it won't be prefixed with #, so:

$(document).click(function (e) {
  if ($(e.target).is("#" + somediv + " *")) {
    // some code
  } 
}

1 Comment

my bad, somediv is a dom element.
0

Try this approach.. Instead of .is() try using equality selector..

<div  class="hide"> 
    <span class="green" id="somediv"> YOOO </span>
</div>​

$('div').click(function(e) {
    if (e.target != this) {
        alert(((e.target.id) == 'somediv'));
    }
});​

CHECK FIDDLE

4 Comments

It doesn't solve my problem. It's a plugin and I have somediv on this.
I don't know the id. It's a plugin, I have to work with $(this).
In any case, did would check whether e.target is the element itself, not one of its descendants.
@sushanthreddy it was there to show that somediv is a dom element. now it's fixed. my bad again. =\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.