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

  • SEARCH
  • Home
  • 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 9215559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:13:51+00:00 2026-06-18T02:13:51+00:00

Possible Duplicate: jQuery: click function exclude children. I have two div s, something like

  • 0

Possible Duplicate:
jQuery: click function exclude children.

I have two divs, something like this:

<div id="parent" style="width: 100%; height: 100%; background-color:red;" />
    <h1>I'm the parent!</h1>
    <div id="child" style="width: 300px; height: 200px; background-color:yellow;">
        </h2>..and I'm the child!</h2>
    </div>
</div>

Additionally, I have the following JavaScript code:

$(document).ready(function(){
    $('#parent').click(function(){
        alert('Parent was clicked');
    });
});

The problem is, if I click on the child, the event is triggered. How can I limit this event to the parent only?

Edit: Just to clarify; I want this action to trigger when the user clicks anywhere in the red. As said in the comments; the h1 is not the parent. Tjirp’s answer did the trick, and there’s a lots of working variants of this solution in the answers.

  • 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-06-18T02:13:52+00:00Added an answer on June 18, 2026 at 2:13 am

    This should work

    $(document).ready(function() {
        $('#parent').click(function(e) {
            if (e.target == this) { 
                alert('Parent was clicked');
            }
        }
    }
    

    This way you won’t have to bind anything to your childs. The click event is propagated to your click handler, and it checks if the target of the click event is indeed the element you added the event on.

    Edit: I was right. this is not the most efficient way, Alessandro Minoccheri answer should be way faster. I updated my code with his.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: click link below a higher z-index div I have two divs, one
Possible Duplicate: jQuery bind click ANYTHING but ELEMENT I have this HTML <div id=outer>
Possible Duplicate: jQuery ajax return value I have this jQuery script: $('#headerSubmit').click(function() { var
Possible Duplicate: Prevent click event from affecting parent jquery I have a DOM structure
Possible Duplicate: jquery on vs click methods I understand that $(parent).on('click', 'element', function(){ ...
Possible Duplicate: jquery highlight the link when clicked I have this nav: <div id=nav>
Possible Duplicate: Get Class List for Element with jQuery If I have something like
Possible Duplicate: jQuery: how do I animate a div rotation? I have a div
Possible Duplicate: Bind multiple events to jQuery 'live' method I have the following function:
Possible Duplicate: jQuery - on() function when a div is clicked, but not when

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.