Tapping on a button within a nested div

My HTML code looks like this:

<div class="col-lg-3 col-xs-6 individual " style="display: block;">
  <button style="position:relative; z-index:4; padding:8px 7px 8px 30px; font-size:16px; font-family: Calibri,Arial,Tahoma; color:black;" class="excel-button">Bajar Tabla</button>
  <!-- small box-->
  <div data-name="cardColor" class="small-box bg-purple">
    <div class="inner">
      <h4 data-name="title">Semana 9</h4>
      <p data-name="dateRange">26-febrero al 02-marzo</p>
    </div>
    <div class="icon"><i data-name="week" class="fa">9</i></div>
    <a class="small-box-footer" data-name="commentFooter">Sin enviar a revisión&nbsp;&nbsp;<i class="text-warning fa fa-exclamation-triangle"></i></a>
  </div>
  <div class="comment-box text-left" style="display: none;">
    <div data-name="comment" class="comment-inner text-danger" style="margin-right:15px">Este reporte no ha sido enviado a revisión</div>
  </div>
</div>

The outer div functions as a clickable element, and I am attempting to enable the inner button to be clickable as well. However, clicking on the button triggers the actions of the div instead of those associated with the button. How can I ensure that when the user clicks the button, it disregards the div element?

Answer №1

Event bubbling occurs when an event is triggered on an element and then bubbles up to its parent elements with the same event handlers. Most events will bubble up, except for a few like focus. This means that if you click a button, the button's click handler will run first, followed by the click handler of its containing div.

To prevent further propagation of the event, you can use the stopPropagation() method on the event object.

Bubbling refers to events traveling upwards in the DOM tree, while capturing involves events propagating downwards.

For more details, check out this resource!

Answer №2

Check this out. Make sure to test by clicking on both the div and the button

$(function(){
  $('div').click(function(event){
    alert('you clicked on the div element');
  });
  
  $('button').click(function(event){
    event.stopImmediatePropagation(); //This prevents the div's click event from triggering
    alert('you clicked on the button element');
  });
});
div{
  padding: 20px;
  background: #f4f4f4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <button>Press Me!</button>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The navigation bar refuses to budge towards the right side

I am having an issue with my navbar alignment. I have set the div as shown, but the navbar is still staying on the left side instead of moving to the right. <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" h ...

Bootstrap 5 is currently validating on submission but neglecting to display hints for invalid fields

After condensing a Laravel application example that utilizes Bootstrap 5.0 and JavaScript, I noticed an issue. The validation functions as expected at the JavaScript level, but Bootstrap fails to highlight incorrectly filled in or empty fields upon failure ...

Is there a way to mix up the sequence of information when replying?

There have been numerous instances where this question has been raised and addressed, such as here, here, and here. However, those discussions pertained to Bootstrap 4, which is approaching its End of Life as of today, 2021-10-20. Given that Bootstrap 5 i ...

What is the best way to define a margin according to the size of the device being used

I am working on an angular/bootstrap web app and need to set a left margin of 40px on md, xl, lg devices and 0px on sm device. I attempted to create a spacer in styles.scss like this: $spacer: 1rem; .ml-6{ margin-left:($spacer*2.5); } Then in my HTML, ...

jQuery - issue with toggling class on label/input when clicking rapidly

I've encountered an unusual issue with my checkboxes. HTML: <div class="f-checkbox"> <label class="f-label" for="f-field"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure cumque voluptatem nisi incidunt praese ...

Updating style of an element by hovering over another

What is the best way to change the CSS of one element when hovering over another element in CSS? We can achieve this using the following code: .example .example2 li:hover .element If our HTML looks like this: <div class='example'><di ...

Avoid displaying hidden images or loading content upon clicking a tab

Check out my website here I have created four tabs on my website, and I want each tab to display a different gallery (Nextgen Pro gallery) when clicked. However, the performance is currently very poor because I am loading all the images at once. I plan to ...

Vue.js Ready event is not firing

In my Vue function, I have two methods. The first method, postStatus, is used to save a post when the user clicks on the save button. The second method, getPosts, is used to retrieve all previous posts from the database for that user. Below is the Vue.js ...

Exploring the power of React Hooks with the useState() function and an array filled

When creating new "Todo" items and modifying the fields, everything works fine. However, my problem arises when trying to retrieve the child data after clicking the "show all objects" button. To better understand my issue, here is a code example: const Co ...

Deliver compressed data in gzip format from a Node.js server to the client using socket.io

I am currently facing an issue regarding determining whether the data being sent back to the client is compressed in gzip format or not. Upon examining my server's output from the command line, I notice the following: debug - websocket writing 3:::{" ...

Reduced artwork slideshow on wide-screen phone orientation

Whenever I switch between portrait and landscape mode on my website, the gallery sizing becomes an issue. It looks good in portrait mode but ends up being too large in landscape mode, especially on my iPhone SE where it doesn't fit properly on the scr ...

Could you explain the distinction between addClass and attr functions in jQuery?

When it comes to adding a class, we have two options at our disposal. $('#x').addClass('test'); Alternatively, we can achieve the same result using the following method: $('#x').attr('class','test'); Th ...

Implementing camera orientation changes in Three.js upon button click

Is there a way to adjust the camera's lookat position by simply clicking a button or link? Below is the code I currently have: HTML: <a href="#" id="testButton">TEST</a> JS: render(); function render() { trackballControls.update(6 ...

Utilize jQuery ajax to target a particular recurring input

I am using HTML code along with another jQuery loop. Within this loop, there is an input element that is also being looped. I am trying to manipulate it using jQuery Ajax... but I am facing an issue where only the first input element works properly. The re ...

The battle between jQuery Ajax and browser URL manipulation

I'm attempting to utilize the YouTube API to retrieve a list of a user's videos. The request URL looks like this: with 'username' representing the correct username. This displays the proper URL in the browser. However, when I t ...

Exploring the incorporation of various local fonts in NextJs version 13

Having trouble adding multiple local fonts to nextjs 13. According to the instructions in the documentation, you should follow these steps for a single file. I attempted to import two files like this: import '@/styles/globals.css'; import localF ...

Adjust the translateZ value according to the dynamic height of the element

A unique rotating cube effect has been developed using 3D css transformations. To achieve this, the following transform is applied to a child element: transform: translateY(-50%) translateZ(-75px) rotateX(90deg) ; In addition, the parent element contain ...

Challenges when sending data to a component in Vue

I'm completely new to using Vue. In one of my components, I have integrated a multiselect component. The issue I am facing is that although I pass a value to my component and then pass it on to the multiselect, most of the time I cannot see that valu ...

Unable to access Handlebars variable within the each block

In my Express.js Handlebars code, I am facing an issue with the accessibility of page.path. {{page.path}} {{#each navigation}} {{page.path}} {{/each}} The problem is that although page.path is accessible outside of the #each block, it does not work wit ...

Automatically trigger canvas object with a click

Is it possible to automate clicking on an HTML5 canvas object for Selenium based automated testing? I came across some resources discussing triggering click events and making clickable objects within a canvas: Trigger Click Clicking Canvas Objects I att ...