Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1080255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:00:00+00:00 2026-05-16T22:00:00+00:00

I have the following code. <html> <head> <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js></script> </head> <div id=hello>Hello <div>Child-Of-Hello</div></div>

  • 0

I have the following code.

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>

<div id="hello">Hello <div>Child-Of-Hello</div></div>
<br />
<div id="goodbye">Goodbye <div>Child-Of-Goodbye</div></div>

<script type="text/javascript">
<!--
function fun(evt) {
    var target = $(evt.target);    
    if ($('div#hello').parents(target).length) {
        alert('Your clicked element is having div#hello as parent');
    }
}
$(document).bind('click', fun);
-->
</script>

</html>

I expect only when Child-Of-Hello being clicked, $('div#hello').parents(target).length will return >0.

However, it just happen whenever I click on anywhere.

Is there something wrong with my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:00:01+00:00Added an answer on May 16, 2026 at 10:00 pm

    If you are only interested in the direct parent, and not other ancestors, you can just use parent(), and give it the selector, as in target.parent('div#hello').

    Example: http://jsfiddle.net/6BX9n/

    function fun(evt) {
        var target = $(evt.target);    
        if (target.parent('div#hello').length) {
            alert('Your clicked element is having div#hello as parent');
        }
    }
    

    Or if you want to check to see if there are any ancestors that match, then use .parents().

    Example: http://jsfiddle.net/6BX9n/1/

    function fun(evt) {
        var target = $(evt.target);    
        if (target.parents('div#hello').length) {
            alert('Your clicked element is having div#hello as parent');
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following file file: <html> <head> <title></title> <script type=text/javascript src=/Prototype.js></script> <script type=text/javascript>
I Have Following codes: <html> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8> <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js></script> <script
I have the following code <html> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <title>Untitled Document</title>
Hi I have the following code: <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <title>Registration Form</title>
I have written the following in a JavaScript method : <html> ... <script type=text/javascript
I have the following code <xsl:result-document href=output1/output3/index.html format=html> <html> <head> <SCRIPT LANGUAGE=JavaScript> function getParams()
I have the following HTML Code <%@ Page Language=C# %> <html> <head> <title></title> </head>
I have the following html and css code: <!DOCTYPE HTML> <html> <head> </head> <body>
I have the following code: HTML <div id='foo'> <input type='checkbox' value='english' name='bar[english]' /> <input
I have the following code in HTML and wanted to replace them using javascript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.