Does anyone know of a JavaScript function that works similarly to clientX and clientY but for tooltips when the mouse moves?

When using plain JavaScript to create a function that moves a tooltip on mouse move, the function works the first time with clientX and clientY, but fails to work when repeated. What could be causing this issue with clientX and clientY? Any suggestions on how to fix this?

const tooltip2 = document.querySelectorAll('.content-section-text + section');
window.onmousemove = function(e) {
  const x = (e.clientX + 5) + 'px',
    y = (e.clientY + 5) + 'px';
  for (var i = 0; i < tooltip2.length; i++) {
    tooltip2[i].style.top = y;
    tooltip2[i].style.left = x;
  }
};
.content-section-text {
  position: relative;
  cursor: default;
}

.content-section-text+section {
  display: none !important;
  background-color: #ECECEC;
  color: #4D4E53;
  font-size: 12px;
  padding: 4px 8px 4px 9px;
}

.content-section-text:hover+section {
  display: block !important;
  position: fixed;
}
<div class="left-content-section">
  <div class="content-section-icon">
    <img src="pic.png" />
  </div>
  <div class="content-section-text">N/A</div>
  <section class="tooltiptext1">Name</section>
</div>

Answer №1

Check out this updated version with multiple DIV elements

document.getElementById("container").addEventListener("mousemove", function(e) {
  if (!e.target.matches(".content-section-text")) return 
  const x = (e.x + 5) + 'px', y = (e.y + 5) + 'px';
  const tooltip2 = e.target.nextElementSibling;
  tooltip2.style.top = y;
  tooltip2.style.left = x;
});
.content-section-text {
  position: relative;
  cursor: default;
}

.content-section-text+section {
  display: none !important;
  background-color: #ECECEC;
  color: #4D4E53;
  font-size: 12px;
  padding: 4px 8px 4px 9px;
}

.content-section-text:hover+section {
  display: block !important;
  position: fixed;
}
div.content-section-text { border: 1px solid black;}
<div id="container">
<div class="left-content-section">
  <div class="content-section-icon">
    <img src="pic.png" />
  </div>
  <div class="content-section-text">N/A</div>
  <section class="tooltiptext1">Name</section>
</div>
<div class="left-content-section">
  <div class="content-section-icon">
    <img src="pic.png" />
  </div>
  <div class="content-section-text">N/A</div>
  <section class="tooltiptext1">Name</section>
</div>
<div class="left-content-section">
  <div class="content-section-icon">
    <img src="pic.png" />
  </div>
  <div class="content-section-text">N/A</div>
  <section class="tooltiptext1">Name</section>
</div>
</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

Retrieve a true value by using either Array.some or _.some

I am looking to retrieve a truthy value from the array.Some other than "true", This is my approach: let category; arduair.aqi_ranges[pollutant].some((item,index)=> { let min = item.range[0]; let max = item.range[1]; if (_.inRange(c,min,max)){ ...

Ensuring the safety of JavaScript requests made to a Web Service

In my project, there is a page that triggers a JSon script when a button is clicked. This script interacts with a web service. To ensure security, the code behind the page generates a script containing an object with a unique code. This code is then added ...

Choosing an option from a dropdown menu by extracting information from JSON content

My goal is to develop a basic jQuery plugin that interacts with a geolocation system to provide data on the location of the user's IP address and then automatically select the corresponding country in an HTML form. Within my HTML code, I have a selec ...

What is quicker: loading SVG images or requesting sprite images?

Is there a recommended approach for creating a basic shape and icon that is used in various sections of the website with different colors? Should I opt for SVG or sprites? However, I wonder if there is a universally accepted solution for this issue. ...

How can I differentiate between an unreachable server and a user navigating away in a $.ajax callback function?

Situation: You have a situation where several $.ajax requests to the server are still in progress. All of them end with xhr.status === 0 and xhr.readyState === 0. Possible reasons for this issue: The server might be down (EDIT: meaning it is unreachabl ...

Navigating a page without embedding the URL in react-router-dom

In my application, I am utilizing react-router-dom v5 for routing purposes. Occasionally, I come across routes similar to the following: checkup/step-1/:id checkup/step-2/:id checkup/step-3/:id For instance, when I find myself at checkup/step-1/:id, I int ...

Encountering a Node.js error while using ssh2: ECONNRESET read error

I have been attempting to utilize npm's ssh2 package to establish an SSH connection and remotely access a machine. The code functions properly for connections from Windows to Linux/MacOS, but encounters issues when connecting from Windows to Windows ( ...

Removing a specific row in a database table and sending a boolean indicator to an API, all while keeping the original object intact

I'm currently working on a spa project using AngularJS and integrating an api with mvvm in C#. One issue I am facing is that when I click the delete button, it deletes the line but I only want to change a boolean flag to true on the server side while ...

Vue-Firebase: A guide to linking multiple Firebase services in a single app

I am facing an issue with connecting to two firebases within the same project. In my project, I have two javascript files that connect to each firebase separately, which seems fine. Here is how I import them: import db from '../FireBase' i ...

Setting a default value for a select option in Angular 2

I am trying to set a default value for an option, acting as a placeholder using this method. It works in pure HTML, but when I implement it with the *ngFor attribute in Angular 2, nothing is selected. Here is the code I use in pure HTML: <select name= ...

Looking for advice on utilizing Node.js and MongoDB to identify platform modifications on a website

I need guidance for a project I'm currently working on. The project involves extracting headers, like the example below in Mongo document format: { "url": "google.com", "statusCode": 301, "headers": { "location": "http://www.goog ...

CSS shrink effect upon hovering

I'm currently working on designing a menu using <ul>/<li> tags along with CSS styles. Here's the progress I've made so far with my menu design: https://jsfiddle.net/gANfS/6/ However, I encountered an issue where if you hover ov ...

Divide text to reduce its width within the confines of a specific height on a div element

I've spent the past week scouring various forums, including stackoverflow, but I haven't been able to find a solution. In my responsive website, I'm using CSS flexbox to display dynamic menu items. Sometimes the text can be quite long, and ...

Using jQuery to select a div element that has been dynamically loaded using ajax, or inserted into the DOM using the html()

Greetings from a newcomer to stackoverflow! After searching for similar questions and answers, I still couldn't find a solution to my specific problem. Here's the situation: I have a webpage that uses ajax to load main content. The function for ...

Is it possible to filter JavaScript Array while ensuring that select options do not contain duplicate IDs?

I am utilizing two datatables with drag and drop functionality. Each row in the datatables contains IDs. When I drag a row from table 1 to table 2, the data is stored in an Array. I have an addArray function that pushes the IDs into the Array, filters out ...

Angular dynamically filling in a table with incomplete object data

I am currently working on a scientific project that involves displaying large amounts of data in tables. The experiments I'm working with have timestamps that follow this format: interface TimeData { time: string; data: {SD: string, SEM: string ...

The UL list-style does not seem to be working properly on the Woocommerce checkout page

After creating my e-commerce website and implementing the woocommerce plugin to display products, I encountered an issue. The main menu pages are all showing up with the list-style, but the pages associated with the plugins are not displaying the list-styl ...

The background image fails to load in ReactJS application

Usually, I have no trouble finding solutions to my problems, but this time I'm really stuck - and I apologize in advance if my question seems silly or trivial. I am currently working on a simple React app where I need to set a background image for th ...

Having difficulty recreating the arrow feature in the bootstrap sidebar

I am currently working on creating the fourth sidebar (from the left) using the templates provided in Bootstrap 5 examples. Everything seems to be falling into place except for one issue - I can't seem to get the arrow to rotate when aria-expanded=tr ...