I am looking for a way to retrieve the ids of all div elements that have the same x coordinate using document.elementFromPoint in JavaScript. Can someone help me with

Currently, I am facing an issue where I have two divs positioned at the same x coordinate. I am attempting to retrieve the IDs of both divs using document.elementFromPoint(). However, I am only able to receive the ID of one div.

var elem = document.elementFromPoint(50, 50);
var elem2 = document.elementFromPoint(50, 250);
console.log(elem.id, elem2.id);
#box1,
#box2 {
  width: 50px;
  height: 50px;
  background: greenyellow;
}

#box1 {
  position: absolute;
  top: 50px;
  left: 50px;
}

#box2 {
  position: absolute;
  top: 50px;
  left: 150px;
}
<div id="box1"></div>
<div id="box2"></div>

Answer №1

You're really close, just make sure to take a look at the documentation where it mentions that your parameters are reversed.

document.elementFromPoint(x, y)

In this case, x represents left, and y represents top.

var element = document.elementFromPoint(50, 50);
var element2 = document.elementFromPoint(150, 50);
console.log(element.id, element2.id);
#box1,
#box2 {
  width: 50px;
  height: 50px;
  background: greenyellow;
}

#box1 {
  position: absolute;
  top: 50px;
  left: 50px;
}

#box2 {
  position: absolute;
  top: 50px;
  left: 150px;
}
<div id="box1"></div>
<div id="box2"></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

Is it possible to dynamically render css using Express.js?

I have a need to dynamically generate CSS with each request. For example, in my Express.js application, I've set up a route like this: /clients/:clientId/style.css When a matching request is received, I want to retrieve the Client from the reposito ...

Utilize PHP to transform various forms of smart quotes

I have been working on a function to convert all types of smart quotes to regular quotes within text. However, the function I have put together still appears to be lacking proper support and design. How can I successfully convert all quote characters? fun ...

Breaking down a string and then retrieving elements from an array

Just diving into the world of Javascript and jQuery, so I have a simple query. I've got a date that I need to break down into a string and then display it as an array. var date = "12/10/2010"; var dateArray = date.split(/); $('#printbox') ...

C# - Implementing JavaScript Object manipulation in .NET Core

As I was browsing, I noticed many similar questions from years ago. Let's kick off this discussion with a simple JavaScript example showcasing how easy it is to modify, read, and write properties in objects using this language. Take a look at the cod ...

What steps can be taken to avoid a component, generated from an array, being re-rendered every time the array is altered but the key remains unchanged

<div className="app"> {[1, 2].map((i) => { return <Test key={i} />; })} </div> In order to achieve the desired outcome where the Test component with key=2 does not re-render when the array is ...

Running a script through the terminal using an electron js application, but the process necessitates the installation of node js

I am currently working on a project that involves running a script through the system terminal using my Electron.js application. However, I am facing an issue with new users who may not be technically proficient and do not have Node.js installed on their s ...

Having trouble with the nth-child CSS property in Bootstrap?

My cards need some styling adjustments - specifically, I want the first and third card in each row to have a top margin of 50px. Strangely, this doesn't seem to work with Bootstrap, but it does with pure CSS. Is there a specific class in Bootstrap tha ...

Encountering issues with running the 'npm run serve' command locally in a Vue project

Trying to develop an app with Vue, I used the npm command. However, when I executed "npm run serve," the messages showed me that I should be running the app at "http://localhost:8080/" and not on "x86_64-apple-darwin13.4.0:". Is there a way to fix this by ...

React-Leaflet: Markers vanishing while navigating the map

I am using the react-leaflet library to create a map. However, I have encountered an issue with markers disappearing when the map is moved on mobile devices, especially those close to the edge of the map container. On my map, I have around 4000 markers an ...

What could be causing the web page to refresh at regular intervals?

As a newcomer to nextjs, I'm puzzled by why my website keeps reloading periodically. I understand that hot code loading is a feature of nextjs and it will reload the page whenever any changes are saved to disk. But how can I determine which specific c ...

How can I trigger the offcanvas opening in Bootstrap 5 through code?

I am having an issue with a bottom offcanvas on my webpage. I want to open it when a card is clicked, but despite trying to set the necessary attributes and using code from the documentation, it only shows the backdrop briefly before immediately dismissing ...

Tips for having the selenium web driver click on a button within a React spotlight (React Joyride)

My app includes a guided tour feature implemented with ReactJoyride. During one step of the tour, progression relies on clicking the 'Ongoing' tab/button (as shown in the image). https://i.stack.imgur.com/MYXYI.png The 'Ongoing' butto ...

Ways to execute a function when a button is clicked with a shared variable?

When creating buttons in HTML to control video speed, I encountered a strange issue. After successfully implementing the buttons for one video, they only worked on a second video and had no impact on the first one. Deleting the second video seemed to resol ...

the three.js code runs smoothly on JSfiddle, but it does not seem to be working properly

Check out this basic three.js code snippet for generating custom geometry http://jsfiddle.net/67Lgzjpb/. After attempting to run this code directly in my browser (not via JSFiddle), an error message pops up: TypeError: geom.computeCentroids is not a funct ...

CSS for Adjusting Parent Height Based on Child Content

I've been working on adjusting the height of the parent class to fit the child class perfectly without overflowing Here is a snippet from my CSS file: Parent &__video position: relative; width: 471px; background: red; border-radius: 12px ...

Aligning Description Item components horizontally in antdLearn how to easily horizontally align Description

Currently, I am utilizing the `antd` Description components. In this scenario, when there is no `title` for the items, the value should be aligned to the left. You can see an example of this alignment in the image below: I have attempted to fix this issu ...

Angular 14.2.9: "Trouble with Form Data Binding - Seeking Assistance with Proper Data Population"

I'm currently using Angular version 14.2.9 and the component library I'm utilizing is: import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; While working on binding data to a form, I encountered an issue where the data wasn't d ...

Exploring an Array Based on User's Input with JavaScript

Looking to implement a search functionality for an array using AJAX. The array is pre-populated with values, and the user will input a value in a HTML text box. If the entered value is found in the array, it should display "Value found", otherwise "not f ...

Managing simultaneous asynchronous updates to the local state

There is a scenario where a series of asynchronous calls are made that read from a local state S, perform certain computations based on its current value, and return an updated value of the local state S'. All these operations occur at runtime, with ...

Why Isn't AJAX Displaying the Output from PHP?

Currently, I am implementing ajax to display a string received from a php file. This particular php file is generating rows for an html table. My goal is to have ajax place these rows within a with a specific id that resides inside a and I want this oper ...