3

I'm trying to write a click function that checks if e.target is contained by a parent element. I've tried using .not() to exclude the element from another $(document).click function that is causing some un desired behavior, but that didn't work. I also tried using .contains(child,parent) but that doesn't work well because it requires the child element to not be a jQuery or JavaScript object. My case is complicated because there are several levels within my parent element and many more outside it.

Here is roughly what I want to do:

$(document).click(function(e) {
    if($.contains($('.dropdown-menu'),$(e.target)) {
        e.stopPropagation();
    } else {
        $('.dropdown-menu').hide();
    }
});

Here is my rendered HTML:

<div class="dropdown">
    <button class="j-descrip-button thin job-dropdown-link" menu="refer">Refer A Friend</button>
    <div class="dropdown-menu right refer-menu" style="display:none;">
        <div class="dd_arrow">
        </div>
        <div class="refer-dropdown-content">
            <form accept-charset="UTF-8" action="/send_refer_a_friend_email" html="{:role=>&quot;form&quot;}" method="post" class="ng-pristine ng-valid">
                <div style="margin:0;padding:0;display:inline">
                    <input name="utf8" type="hidden" value="✓">
                    <input name="authenticity_token" type="hidden" value="{{authenticity_token}}">
                </div>
                <div class="form-group" style="display: none;">
                    <label for="company_name">Company Name</label>
                    <input class="form-control" id="company_name" name="company_name" type="text" value="{{j.company_name}}">
                </div>
                <div class="form-group" style="display:none;">
                    <label for="job_title">Job Title</label>
                    <input id="job_title" name="job_title" value="Python Developer" type="text" class="form-control">
                </div>
                <div style="font-size: 20px; margin-bottom: 10px;">
                    <b>You're an awesome friend!</b>
                </div>
                <div class="form-group">
                    <label for="user_full_name">Your Name</label>  <!-- is this the best way to do this? -->
                    <div>
                        <input class="refer-text-field" id="user_full_name" name="user_full_name" type="text" value="{{current_user_name}}">
                    </div>
                </div>
                <div class="form-group">
                    <span>
                        <label for="friend_email">Your Friend's Email</label>
                        <em style="font-size: 10px;">use commas for multiple emails</em>
                    </span>
                    <div>
                        <input class="refer-text-field" id="friend_email" name="friend_email" type="text">
                    </div>
                </div>
                <div class="prof-link" style="display: none;">
                    <label for="prof_link">Profile Link</label>
                    <input class="form-control" id="prof_link" name="prof_link" type="text" value="{{profile_link}}">
                </div>
                <input class="refer-submit-btn" name="commit" type="submit" value="Send to My Friend(s)">
            </form>
        </div>
    </div>
</div>
5
  • @squint I do think that he meant a different question. He asked if the target div is a child to any parent, and not if it is a parent to any children. Anyway, ACIDSTEALTH, you can use .parent(). Here's the API (api.jquery.com/parent). And I can tell you that every element will have a parent, except the html I guess. Commented Jul 13, 2015 at 22:29
  • Use $.contains, but pass DOM elements instead. $.contains($('.dropdown-menu')[0], e.target) Commented Jul 13, 2015 at 22:29
  • @Ted: Yeah, I reopened it a few seconds after closing it. Commented Jul 13, 2015 at 22:31
  • 1
    Or it seems you can do $(".dropdown-menu").has(e.target).length Commented Jul 13, 2015 at 22:33
  • $('.dropdown-menu').has(e.target).length > 0 seems to work fine. I tried .has and .find and couldn't get them to work. Commented Jul 13, 2015 at 22:39

2 Answers 2

3

Use find

$(document).click(function(e) {
    if($('.dropdown-menu').find($(e.target))) {
        e.stopPropagation();
    } else {
        $('.dropdown-menu').hide();
    }
});

I think you can use $(e.target) or e.target, there is no difference.

Sign up to request clarification or add additional context in comments.

6 Comments

I think that normally this would be correct, but for whatever reason it isn't working in my situation. I think it may be because there are many instances of $('.dropdown-menu') and it doesn't know which one to search in.
weird... maybe target is the same as .dropdown-menu or target is parent of .dropdown-menu. Try in this stackoverflow page using the console (F12) this code $('.vote').find('[value="31394679"]') it works ok despite there are two .vote
You should show us your html for us to properly help you
That's why it wasn't working right I think. Sometimes e.target is $('.dropdown-menu'). See the last edit to my question for HTML.
ok, in that case you can refer to stackoverflow.com/questions/2828019/…
|
2

You can use the parent function like this, since it will always be a child of something, you can check to see if it is or is not in the body tags:

$('button').click(function(){
    var x = $(this).parent();
    if (!x.is('body')) {
        console.log("It has it!");
    } else {
        console.log("It does not have it!");
    }
});

Here is a working example: http://jsfiddle.net/5cjp47c7/

Or if you know what the parent class or id name is you can use this:

if ( $(".child-element").parents("#main-nav").length == 1 ) { 

   // YES, the child element is inside the parent

} else {

   // NO, it is not inside

}

Comments

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.