Does MouseOver event not bubble up on overlapping div elements?

Employing jQuery 2.1 along with Meyer's 2.0 CSS reset script, specifically targeting IE9+ and modern browsers.

In this scenario, I have created two divs that are overlapping. I have set up event listeners for both the mouseover and mouseout events on these divs. You can view the demonstration here: http://jsfiddle.net/vbkjL/1/

The desired outcome is to have mouseover events trigger on both divs simultaneously

However, what actually happens is that the mouseover event only triggers on the top div.

I am seeking guidance on how to ensure that the mouseover event propagates to the bottom div as well.

Here is the HTML structure:

<div id="container">
    <div id="top"></div>
    <div id="below"></div>
</div>

jQuery Code:

 $("#top")
     .mouseover(function () {
     console.log("top");
 })
     .mouseout(function () {
     console.log("top out");
 });

 $("#below")
     .mouseover(function () {
     console.log("bottom");
 })
     .mouseout(function () {
     console.log("bottom out");
 });

CSS Styles:

#top {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 100px;
    border: 1px red solid;
}
#below {
    position: absolute;
    top: 0;
    left: 0;
    width: 32px;
    height: 32px;
    border: 1px blue solid;
}

Answer №1

To ensure that the mouseover on #below triggers the same event on #top, you need to make #below a child of #top.

<div id="container">
    <div id="top">
        <div id="below"></div>
    </div>
</div>

In your current HTML structure, there is no hierarchical relationship between the two divs for bubbling or capturing events since they are not nested.

A quick recap of event bubbling and capturing from Quirksmode:

Event capturing

When using event capturing:

               | |
---------------| |-----------------
| element1     | |                |
|   -----------| |-----------     |
|   |element2  \ /          |     |
|   -------------------------     |
|        Event CAPTURING          |
-----------------------------------

The event handler of element1 fires first, and the event handler of element2 fires last.

Event bubbling

When utilizing event bubbling:

               / \
---------------| |-----------------
| element1     | |                |
|   -----------| |-----------     |
|   |element2  | |          |     |
|   -------------------------     |
|        Event BUBBLING           |
-----------------------------------

The event handler of element2 fires first, and the event handler of element1 fires last.

Check out this demo to see it in action. Feel free to clarify if I misunderstood your question.

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

Utilizing jQuery UI to dynamically alter CSS classes

I'm currently working on a project and could use some assistance. I have divs that are droppable using jQuery UI. While everything is functioning properly and I can drag and drop, I am looking to modify the opacity of the div I am dragging by changing ...

unable to execute PHP code

I am attempting to execute a PHP file that will update a database. On Chrome, I am encountering this error: https://i.sstatic.net/3ruNL.png This is the code I have in my index.html file: <!DOCTYPE html> <html> <body> <input type ...

Attention all beginners: there is an issue with the navigation bar where the section below is overlapping it. Additionally, assistance is needed in understanding how to set limits for

Greetings to all. I'm encountering an issue while setting up my first Bootstrap Nav bar. Although I've managed to make it work, the section right below it is overlapping the navbar (I can only see the navbar if I use a z-index of -1, but then the ...

Attempting to implement ajax for form submission, however finding that the $_POST array is coming back as empty

I'm attempting to send JavaScript arrays to a new page using Ajax. Although there are numerous questions on this topic on Stack Overflow, I have decided to implement Ajax in the following manner after examining various answers: var test = {}; test[& ...

Incorporate React Pages into Discord Js integration

Chat command "911" generates a bot help list with an embed and three reaction buttons: "forward", "backward", and "delete". The functionality seems fine, but I'm encountering an issue where the reaction buttons only work once. For instance, if I navig ...

Combining two containers in CSS

I am currently working on building a website design, and I have a menu positioned on the right side of the layout. Below is the code for the menu along with an explanation. #rightmenu { border-top: 1px solid #000000; border-right: 1px solid #00000 ...

Is there a way to horizontally navigate a pallet using Next and Prev buttons?

As someone new to development, I am looking for a way to scroll my question pallet up and down based on the question number when I click next and previous buttons. In my pallet div, there are over 200 questions which are dynamically generated. However, th ...

"Troubleshooting issues with the Updater function within the variable context API in React Native

I am in need of assistance as I am currently developing a survey system. The challenges I face involve creating surveys with an undetermined number of questions, requiring dynamic rendering based on the type of question (select, text, numeric, etc.). To ad ...

Uncovering the status code from a WebSocket upgrade request using the socket.io client

I have updated the upgrade event listener in my node.js code as follows: server.on('upgrade', (req, socket, head) => { socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); socket.destroy(); return; } Is ther ...

Tips for executing a series of tasks before a node js process is terminated

Dealing with signal from an external file in a JavaScript file. I have a shell script that stops all running processes. When my node process is stopped or killed, I need to perform a specific task before allowing the node to stop/killed. In my JavaScript ...

Troubleshooting Problems with CSS Three-Column Layout

Struggling with aligning domain names in a 3 column setup, I have been attempting this for some time now without success. I am new to this and could use some guidance. Here is the code: TPL file <div> <p class="center">{foreach key=num it ...

Transforming segments into divisions

I recently designed a website using sections instead of divs in order to achieve the desired alignment of the divs within the same container. However, I am facing difficulties and confusion in implementing this approach. Currently, my divs collapse on top ...

Resolving Typescript jQuery AJAX Navigation Problem

Hello dear Earthlings, The code snippet below is currently implemented on my website brianjenkins94.me for handling basic navigation functionalities. After some recent changes, I encountered an issue with the #nav.on("click") event handler not working as ...

Is it possible to manipulate div elements with JavaScript while utilizing node.js?

It seems like I'm missing something obvious here, as I'm working with node.js and I have correctly linked a javascript file (alert("hello"); works). However, I can't seem to make any changes to the DOM. Here's a simple example: jade: ...

Exclude specific outcomes within a nested document in MongoDB

I have a situation where I need to query a list of items and only retrieve the ones that correspond to a specific ID in the provider_cost_dict. Essentially, if I input providerId = 10001, then only the items with a matching entry in the provider_cost_dict ...

How can you display an image with text on hover in a simple way?

Is there a way to display an image (a company logo sized at 150 x 100 px) when a user hovers over the company name? The image should appear on the right side of the name. I am uncertain whether this can be achieved with CSS3 alone or if I would need to us ...

Purging data when no input is detected in React.js

I need to figure out a reliable way to detect when my input field has been cleared so that I can clear the associated data. Currently, I am using the logic if (searchTerm.length === 0) to check for a cleared input. However, I have also experimented with i ...

When trying to show a Vue view, there was an issue with reading properties of null, specifically the 'style' property

I am experiencing an issue with a header that has an @click event in the body. Instead of displaying a Vue view upon clicking, I am encountering the following error: Cannot read properties of null (reading 'style') Upon researching on Stack Ove ...

What is preventing me from adjusting the padding of the mat-button?

Trying to adjust the default padding of a mat-button, but encountering issues with changing the component's style. Any suggestions on how to subscribe to the default padding (16px)? I've attempted modifying CSS properties to set the padding of a ...

What is the best way to position four responsive divs within a container?

Currently working on a responsive website and encountering challenges with the floating container. The goal is to have four divs positioned directly next to each other without any gaps, while also ensuring they stay in place on smaller screens or windows ...