The onmouseover event is malfunctioning on the entire div element specifically in the Chrome browser

When I created a grid of small 10px divs and added a mouseover listener to each one, I noticed an issue in Chrome. If I entered a div from the bottom with my mouse pointer, the event listener wouldn't trigger until I was about halfway up the div. This strange behavior only occurred when using small divs in Chrome, as making the divs larger or switching to Firefox resolved the problem.

<!DOCTYPE html>
<html>
<head>
<script>

window.onload = function() {

    for(var row = 0; row < 10; row++) {

        var rowDiv = document.createElement("div");
        rowDiv.style.lineHeight = 0;

        for(var col = 0; col < 10; col++) {

            var cellDiv = document.createElement("div");
            cellDiv.style.height = "10px";
            cellDiv.style.width = "10px";
            cellDiv.style.display = "inline-block";
            cellDiv.style.backgroundColor = "green";
            rowDiv.appendChild(cellDiv);
            cellDiv.onmouseover = (function(cell) {
                return function() {
                    cell.style.backgroundColor = "yellow";
                };
            })(cellDiv);
        }
        document.getElementById("container").appendChild(rowDiv);
    }
}
</script>
</head>
<body>
    <div id="container"></div>
</body>
</html>

Answer №1

There was a specific issue with setting style.lineHeight to 0 in this scenario, although it was a necessary step. However, there is a solution available to rectify the problem that arose from this action.

Initially, let me provide a brief summary of the original context. I had designed a grid consisting of div elements where I aimed for all adjacent divs to be in contact with each other. Additionally, individual mouse event listeners were required for each div. To ensure that the row divs (which serve as containers for these individual divs) touched each other, I opted to set their line heights to 0. Unfortunately, this led to the mouse event listener functioning only on the upper half of each div, excluding those in the bottom row.

The issue can be resolved by utilizing the overflow property. By simply setting overflow to hidden for each row div, the mouse event listeners will cover the entire surface area of their respective divs.

<!DOCTYPE html>
<html>
<head>
<script>

window.onload = function() {

    for(var row = 0; row < 10; row++) {

        var rowDiv = document.createElement("div");
        rowDiv.style.lineHeight = 0;
        rowDiv.style.overflow = "hidden";

        for(var col = 0; col < 10; col++) {

            var cellDiv = document.createElement("div");
            cellDiv.style.height = "10px";
            cellDiv.style.width = "10px";
            cellDiv.style.display = "inline-block";
            cellDiv.style.backgroundColor = "green";
            rowDiv.appendChild(cellDiv);
            cellDiv.onmouseover = (function(cell) {
                return function() {
                    cell.style.backgroundColor = "yellow";
                };
            })(cellDiv);
        }
        document.getElementById("container").appendChild(rowDiv);
    }
}
</script>
</head>
<body>
    <div id="container"></div>
</body>
</html>

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

`Can you explain the contrast between a Firefox instance launched by Selenium WebDriver versus a browser instance manually launched?`

Currently, I am in the process of automating a web application using Selenium WebDriver. The unique challenge I have encountered is related to the behavior of a dropdown menu causing page elements to hide under a floating menu. Interestingly, this issue on ...

Guide to manipulating DOM elements with Angular.js: inserting or deleting elements using createElement

Within my Angular directive, I am dynamically generating an external script from the DOM for a specific object within a list. This script includes both a 'script' tag and div content. While I am able to successfully add the script, I am encounter ...

What is the best way to capture user input using an onClick event in JavaScript and then display it on the screen?

I'm in the process of upgrading a table, and while most of it is working fine, there is one function that's giving me trouble. I want this function to return an input with an inline onClick event. The actual return value is displaying correctly, ...

Is there a way for me to determine if an image created with Image() has had its source modified?

Is there a way to track changes in the src attribute of images created using the Image constructor on a webpage without needing the image to be fully loaded? I have attempted to override the Image.onload method, but it does not log anything: Image.prototy ...

Avoiding Webpack externals when using library components / fragments

Using Webpack has been a game-changer for us when it comes to writing isomorphic Javascript. It allows us to seamlessly switch between using npm packages on Node.js and utilizing browser globals during bundling. If I want to include the node-fetch npm pac ...

Adding clickable padding to a Draft.js editor can enhance the user experience and make the editing process

Is there a way to apply padding to the Draft.js Editor so that clicking on the padding area selects the Editor? If I add padding directly to the container div of the Editor, the padding displays properly but clicking on it does not enable writing in the E ...

Add the useState hook from React to an array

Hey there, I need some help with my code. I am trying to add an element to my array, but it's not working as expected. Can you take a look at what I have written below? const [datesState, setDisabledData] = useState([ format(new Date(2021, 4, 1) ...

Transmit information to PHP via JSON with Cross Domain communication

My code is server1 and PHP code is server2. These servers are not connected. What issues can arise from this setup? var req = new XMLHttpRequest(); req.open("POST", "http://www.example.com/form/data.php", true); req.setRequestHeader("Content-type", "ap ...

What is the best way to adjust my content to be mobile-friendly with the Material UI drawer?

I am facing an issue with adjusting the content in Form.jsx based on the movement of the drawer. When I expand the Drawer by clicking on the menu icon, the content inside the body does not move along with it. How can I make sure that the content moves acco ...

Tips for triggering animations on nested content when the parent slide is animated and enters the viewport without relying on traditional scrolling methods

I would like to implement a JavaScript solution that will animate the content of a nested DIV within a parent slide when the parent slide comes into view. Currently, the content in the nested DIV only animates when a scroll command is triggered after the ...

ReactJS Components failing to load on initial site visit, only appearing after refreshing for the second time

var img; var dateFormat = require('dateformat'); var count; let arrayIMG = [] var storage = firebase.storage(); var storeRef = storage.ref('images/') const config = { ... }; if (!firebase.apps. ...

Activate a Python Selenium function to click on a datepicker element

Looking for some help with selenium as I navigate my way around passing values into a form. While I've managed to successfully input values into regular text boxes, I'm struggling with the onclick datepicker function. Here's what I have so f ...

I'm all set to launch my express js application! What are the major security concerns that I need to keep in

As a beginner in deploying express applications, I find myself lacking in knowledge about the essential security measures that need to be taken before launching a web application. Here are some key points regarding my website: 1) It is a simple website ...

Sending a 2-dimensional array from JavaScript to the server using AJAX

My JavaScript code involves working with a 2D array. var erg = new Array(); for (var i in val) { erg[i] = new Array(); for (var j in val[i]()) { erg[i][j] = val[i]()[j](); } } However, I encountered an issue where only the 1D array ...

Selecting a specific section from a JSON data reception

Upon executing a POST request to , the resulting response typically looks like this: { "success": true, "data": { "url": "http://i.imgflip.com/1nciey.jpg", "page_url": "https://imgflip.com/i/1nciey" } } Is there a way for me to output the s ...

Custom JSON filtering

I have a JSON object called Menu which contains various menu items for my system that I am developing using VueJS + Vuetify. I need to filter these menu items based on the "text" field, regardless of position in the text and without differentiating between ...

Raycasting in Three.js is ineffective on an object in motion

Working on a project that combines three.js and typescript, I encountered an issue while attempting to color a sphere by raycasting to it. The problem arises when the object moves - the raycast doesn't seem to acknowledge the new position of the objec ...

Utilizing Vector3 for Parametric Geometry calculations

As I update a script to a newer version of three.js, I encountered an issue with ParametricGeometry. The error message "THREE.ParametricGeometry: Function must now modify a Vector3 as third parameter" keeps appearing. Below is the section of code causing t ...

JavaScript - Delayed Text Appearance with Smooth Start

I want to create a landing page where the text appears with a slight delay - first, the first line should be displayed followed by the second line. Both lines should have an easing effect as they appear. Below is a screenshot of the section: https://i.sst ...

Switch to a different form when clicked using JavaScript

Can you assist me with my code issue? The scenario is as follows: I have two forms - one for registration and one for login. I want to dynamically replace the login form with the register form when the user clicks on the "Sign up" link. Similarly, if the ...