Highcharts - resolving cross-browser e.Offset discrepancies in mouse event detection on charts

I need to determine if the mouseup event is inside the chart and display the coordinates of the point. The code works in Chrome but not in Firefox due to the lack of the event.offset property.

  jQuery(chart.container).mouseup(function (event) {
     eoffsetX=event.offsetX;
     eoffsetY=event.offsetY;
     if (eoffsetX > chartX.plotLeft && eoffsetX < chartX.plotLeft + chartX.plotWidth && eoffsetY > chartX.plotTop && eoffsetY < chartX.plotTop + chartX.plotHeight) {
        alert ("The clicked x,y point is "+chart.xAxis[0].toValue(eoffsetX, 0)+" , " + chart.yAxis[0].toValue(eoffsetY, 0));
     }

After trying different solutions found online, I modified the code but it still doesn't work for Firefox / IE:

  jQuery(chart.container).mouseup(function (event) {
      if(event.offsetX === undefined) {
            eoffsetX=(event.clientX - jQuery(event.target).offset().left);
            eoffsetY=(event.clientY - jQuery(event.target).offset().top);
        }
        else {
            eoffsetX=event.offsetX;
            eoffsetY=event.offsetY;
        }
     if (eoffsetX > chartX.plotLeft && eoffsetX < chartX.plotLeft + chartX.plotWidth && eoffsetY > chartX.plotTop && eoffsetY < chartX.plotTop + chartX.plotHeight) {
        alert ("The clicked x,y point is "+chart.xAxis[0].toValue(eoffsetX, 0)+" , " + chart.yAxis[0].toValue(eoffsetY, 0));
     }      

Any suggestions for a cross-browser solution?

Answer №1

Another option is to utilize the following method:

 let xPosition = typeof(event.offsetX)==='undefined' ? event.clientX:event.offsetX,
     yPosition = typeof(event.offsetY)==='undefined' ? event.clientY:event.offsetY;

Answer №2

After a brief moment of contemplation, the solution to my query materialized before me. In an effort to assist others who may encounter similar issues, I am sharing the remedy below:

All one needs to do is replace

jQuery(event.target).offset().left
with jQuery(this).offset().left

as well as

jQuery(event.target).offset().top
with jQuery(this).offset().top

Et voilà! Problem solved!

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

Encountering issues with Bootstrap modal functionality within a Laravel blade template

I am facing an issue with displaying a modal when a specific link is clicked. The problem is that the modal appears briefly for less than a second and then disappears again. How can I ensure that the modal remains stable? Below is my code: <!DOCTYPE ...

How can I determine if a URL in react js includes a specific string?

I am working on a project where I need to check if a given URL contains the string youtube using react hooks in React JS. This is what my current code looks like: useEffect(() => { let srcLink = "www.google.com/https://img.youtube.com/vi/h9-qcy3HQn ...

The React-Toastify module is missing

Having issues with react toast plugin displaying an error. Prior to attempting to use the toast feature, I executed this command: npm install --save react-toastify The following error is being shown in Google Chrome Console: ERROR in ./src/Pages/Login ...

Vue js version 2.5.16 will automatically detect an available port

Every time I run the npm run dev command in Vue.js, a new port is automatically selected for the development build. It seems to ignore the port specified in the config/index.js file. port: 8080, // can be overwritten by process.env.PORT, if port is in u ...

Implementing Ajax functionality in MVC 3 to return a partial view

I wanted to express my gratitude for this invaluable site that has taught me so much. Currently, I am working on an MVC3 component where I need to populate a selectlist and upon user selection, load a partial view with the relevant data displayed. Everythi ...

When accessing req.user in code not within a router's get or post method

Is there a way for me to access the data in the User schema outside of a post or get request? I am asking this because I would like to use this information elsewhere. The user schema is defined as follows: const mongoose = require('mongoose'); c ...

How can you set a condition for when no matches are found in a list filter?

I recently came across a fascinating example on W3 Schools that taught me how to implement a search filter. Below, I have customized it for everyone's reference. I am interested in modifying it to only display a list item with the message: No matche ...

The function ajax does not recognize response.forEach as a valid function

When I try to use ajax for fetching data from MySQL using PHP and JavaScript, I encounter a function error stating "response.forEach is not a function". Despite looking through various posts on this issue, none of the suggested solutions have been able to ...

Is using canvas the best option for creating simple image animations with JavaScript?

I am currently working on a basic animation project using JavaScript. This animation involves switching between 13 different images per second to create movement. I have created two simple methods for implementing this animation. The first method involves ...

Java REST service remains out of reach for JavaScript fetch call

Currently, I am in the process of learning about REST API. My goal is to make a call to a POST service implemented in Java from Javascript using fetch. However, I have encountered an issue where the request fails to reach the service whenever the @Produces ...

Issues with Javascript functionality on aspdotnetstorefront site

Lately, I've been facing some challenges with my Javascript and jQuery codes when trying to implement them in the storefront. Despite attempting different methods to create a slider, none seem to work within the store environment - even though they fu ...

updating the count of items in a react virtual list

Popular libraries such as react-virtualized, react-window, and react-virtuoso all include an item count property similar to the one shown in the code snippet below from material-ui. However, this property is typically located within the return statement. ...

Configuring CORS in an Angular JS controller

Having a controller with a service that retrieves JSON from another server, I encountered the following issue: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:somesite.com. This can be fixed by moving the ...

Error message displayed in jQuery dialog box

My dilemma arises when attempting to continuously call a dialog box for displaying and submitting an enquiry form on my website. Upon the first submission, everything seamlessly goes through. However, clicking the GO button (as shown in the HTML below) tri ...

Using postMessage to communicate with the localstorage of an iframe from a separate domain

I have a question regarding the challenges of using localStorage for performance reasons and any potential workarounds. From what I gather, once a reference to localStorage is detected on a page (at compile time?), it blocks the thread to read data from di ...

Setting the "status" of a queue: A step-by-step guide

I created a function to add a job to the queue with the following code: async addJob(someParameters: SomeParameters): Promise<void> { await this.saveToDb(someParameters); try { await this.jobQueue.add('job', ...

Challenges with window opening function while already in fullscreen view

Unsure of what might be causing the issue, but here's the problem... My code to open a new window is as follows: var opts = 'location=0,toolbar=0,menubar=0,scrollbars=0,resizable=0,height=450,width=300,right=350'; window.open('/' ...

AngularJS directive that performs asynchronous validation upon blur

I'm working on developing a directive that validates email addresses provided by users through asynchronous web requests. While the functionality is sound, I've encountered an issue where asynchronous calls are triggered every time a user types a ...

Unusual express middleware usage in NodeJS

app.use(function(req,res,next){ console.log('middleware executed'); next(); }); app.get('/1',function(req,res){ console.log('/1'); res.end(); }); app.get('/2',function(req,res){ console.log('/2'); res.end() ...

PHP not delivering a variable via AJAX

I've noticed that there are similar questions on this platform, but I've spent my entire day researching and fixing bugs to figure out why my ajax code doesn't return a response from the php file. All I need is for it to notify me when a use ...