Skip to content
the poor coder

jquery click on parent but not child

Poor Man
Published
Reading time
2 min read
On this page

jQuery Click on Parent but not Child#

As a web developer, you may come across a situation where you want to trigger an event when the user clicks on an element's parent but not the child element. In this case, you need to apply some logic to check whether the clicked element is the parent or the child.

Let's say we have the following HTML structure:

Text
<div id="parent">
  <h3>Click Me</h3>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>

In this example, we want to trigger an alert when the user clicks on the parent div, but not when they click on the h3 or p elements inside it.

Method 1: Event Propagation#

The first method to solve this problem is by using event propagation. When a user clicks on a child element, the event bubbles up to its parent elements. We can use this fact to check whether the clicked element is the parent or not.

We can attach a click event listener to the parent element and check whether the clicked element is the parent or not using the event.target property.

Text

$('#parent').click(function(event) {
  if (event.target.id === 'parent') {
    alert('Parent clicked!');
  }
});

In this code, we are attaching a click event listener to the parent div. When the user clicks on any element inside the parent div, the event bubbles up to the parent div, and our event listener gets called.

We are then checking whether the clicked element is the parent div or not using the event.target.id property. If it is the parent div, we trigger an alert.

Method 2: Stop Propagation#

The second method is to stop the event from propagating when the user clicks on a child element. We can do this by using the event.stopPropagation() method.

We can attach a click event listener to each child element and stop the event from propagating when they are clicked. We can then attach a click event listener to the parent element without worrying about the child elements triggering it.

Text

$('#parent h3, #parent p').click(function(event) {
  event.stopPropagation();
});

$('#parent').click(function(event) {
  alert('Parent clicked!');
});

In this code, we are attaching a click event listener to each child element (h3 and p). When the user clicks on them, we are stopping the event from propagating using the event.stopPropagation() method.

We are then attaching a click event listener to the parent div. When the user clicks on it, our event listener gets called, and we trigger an alert.

Conclusion#

In conclusion, there are two methods to trigger an event when the user clicks on an element's parent but not the child element. You can use event propagation or stop propagation to achieve this effect.

Choose the method that suits your needs, and happy coding!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC