The hover effect is not activated by the mouse movement event

I previously encountered an issue related to flickering with an absolute div when moving my mouse, which I managed to resolve by increasing the distance between my mouse pointer and the div.

Now, I am working on the next phase of my project:

My goal is to make an AJAX call from the server to dynamically set the HTML content of a div based on the value or content of the image when the mouse enters the image area only once.

This is what I have tried so far:

var $divt = $('div#test')
$('img#sorc').on({
    hover: function (e) {
        alert('enter'); // This does not work
    },
    mousemove: function (e) {
        $divt.css({
            'display': 'block',
                'top': e.pageY,
                'left': e.pageX + 15
        });
    },
    mouseleave: function () {
        $divt.css('display', 'none');
    }
});

I attempted to use hover to trigger the AJAX call only once when the user's mouse enters the image area. However, the desired result was not achieved as the alert box did not trigger. While I could preload the div with data from the server, I prefer to keep my code dynamic and reusable without hardcoding anything. My objective is to execute the AJAX call only once when the mouse enters the image area, rather than triggering it every time the mouse moves. How can I accomplish this?

Answer №1

If you want to trigger a $.ajax function when the mouse enters a specified area and track the x,y coordinates within the area, then change the CSS on mouseout without tracking x,y anymore, the following code snippet may be helpful:

<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="./jquery-1.11.3.min.js"></script>
</head>
<body>
<img id="sorc" src="./1.jpg" alt="">
<script type="text/javascript">
$("img#sorc").hover(function() {
alert("in") ;
$("img#sorc").mousemove(function(e) {
console.log(e.pageX) ;
console.log(e.pageY) ;
});
}, function() {
alert("out") ;
$("img#sorc").unbind(onmousemove) ;
});
</script>
</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

Simple method to send information from an MVC controller to jQuery.ajax

We have a project using ASP.NET MVC that involves sending form data to a controller via jQuery.ajax. Sometimes, the controller may encounter exceptions due to incorrect SQL statements. I want to catch these exceptions in a TRY CATCH block and provide detai ...

Fixing the bug in the circle text animation

I followed the instructions in this tutorial and implemented the following code: Splitting(); * { margin: 0; padding: 0; box-sizing: border-box; font-family: monospace; } body { display: flex; justify-content: center; align-items: center; ...

displaying date picker on button click with angular bootstrap ui

I am trying to display an AngularJS Bootstrap calendar only upon clicking a button, and I want to restrict it from appearing when clicking on the input field. Below is my code: <input type="text" onkeydown="return false;" ...

Tips for preventing harmful messages in a chat room app

As I am working on a chat room website, users are able to input any content they want into the entry field and then send it to all online users. However, I have concerns about security - what if malicious individuals try to inject harmful HTML or JavaScrip ...

Retrieve the id within the parentheses for each checkbox that is checked and re-check each time a checkbox is selected using Kendo UI

Working with the tricky kendo-ui has made adding selectors quite challenging; however, my current task involves simply obtaining the inner contents of the id selector upon clicking an input checkbox element. Specifically, I need to extract the text between ...

The readyState property of XMLHttpRequest for abort and timeout detection

Is there a way to determine the state of an instance of XMLHttpRequest that has timed out or been aborted without any action taken during the onTimeout events? For example, when a request results in a 404 error, you can check the status property after the ...

Showing information to several classes using JavaScript

I am currently developing a "Gamebook Engine" that enables users to set a custom username. The user name is extracted from an input element with the id="setUserNameInput" and stored using the function setUserName(). It is then displayed or loaded into an e ...

What is the method in JavaScript for a child function to trigger a Return statement in its parent function?

I have encountered a unique problem. I need to retrieve some data downloaded via ajax and return it, but neither async nor sync modes are fetching the data in time for the return. Is there a way to call the return from a child function to the parent func ...

Difficulty encountered with changing font color on CSS

I'm having trouble fixing the text color, it's currently white and blending in with the background. I've set the color to #1a6eb6 for both the TEXT element and the submenu list items, but it's not working. Can someone assist me with thi ...

Retrieve image data by capturing the image from the input field and showing it on the screen

I've been searching online and can't seem to find a clear answer on whether this task is possible. Here's what I want to achieve before submission: When a user chooses a file for upload, I aim to extract the image data and convert it into b ...

Tables lacking borders where rowspans are present

I'm currently using Firefox and have the following code snippet: html, body { width: 100%; height: 100%; } * { box-sizing: border-box; } body { padding-left: 20px; padding-right: 20px; } .visitentabelle { border: 2px solid black; ...

Using Typescript to establish a connection between ngModel and an object's property

Let's talk about how we can dynamically bind an input to an undefined property in an object. For example, we have an object named user: let user = {}; How can we bind an input to a property that doesn't exist yet? Like this: <input [(ngMode ...

What could be the reason for the malfunction of this AngularJS data binding feature?

I am trying to create an angularjs filter that outputs HTML, similar to what is discussed in the link, but I am encountering issues. In my HTML code, I have: <ul> <li ng-repeat="book in books | filter:query"> {{book.title}} ...

What is the recommended depth in the call stack to utilize the await keyword for an asynchronous function?

My knowledge of async functions in TypeScript/React is fairly basic. I have two API calls that need to be made, and I am using async functions to call them from my UI. It's crucial for these calls to have completed before rendering the component corre ...

DOJO Dialogue Box Report - Problem

I'm currently facing a challenge with incorporating javascript into the dialog box of my application. Here is an example of the code I am struggling with - var profileDialog1 = new Dialog({ title: "Create Profile", style: "width: 700px;he ...

Refreshing data attribute following an ajax request

I have this page with a list of various job listings. Each job listing has a button labeled "Paid" that includes a data-paid attribute indicating whether the job is paid or not. Whenever the "Paid" button is clicked, it sends a request to my route which u ...

Is there a way for me to modify the formatting of the text that I

Can you help me transform a text that looks like this: '?category=1,2&brand=1' into something like this: '?categories[]=1&categories[]=2&brands[]=1'? I'm not quite sure how to make this change. ...

The concept of IFA on Android and accessing it through the browser/javascript

IFA (Identifier for Advertisers) is a new feature in iOS 6 that functions as a unique ID for tracking user context anonymously and improving the overall experience. My inquiries mainly focus on the realm of web development (specifically mobile browser/jav ...

What is the process for incorporating a unique Mongo expression into a JSON object?

I'm currently trying to figure out how to add a specific Mongo command to my JSON object. Normally, adding regular strings or objects is straightforward, but I'm struggling with this particular command: $set : { "author" : req.body.name } Simpl ...

Adding a new column to a table that includes a span element within the td element

I am attempting to add a table column to a table row using the code below: var row2 = $("<tr class='header' />").attr("id", "SiteRow"); row2.append($("<td id='FirstRowSite'><span><img id='Plus' s ...