Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
How do you detect if the cursor is directly above text using javascript?

I want to change an element when the cursor is only directly above some text. For instance :

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Trigger the event when on this text but not the blank space after.

I don't want to trigger this change when the cursor is in the div or paragraph and above blank space, like the space at the end of the end of this paragraph. The problem is that elements formated in rectangles and that includes this blank space.

I don't know if this is possible and I don't really know when to start.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

make sure the element wrapping the text is set to display: inline; and then listen for the mouseover event like so:

let element = document.querySelector('.selector');
element.addEventListener('mouseover', () => {
   // your code here.
});
about 4 years ago · Juan Pablo Isaza Report

0

As stated in the following link:

Note: An inline element does not start on a new line and only takes up as much width as necessary.

Encapsulate the text within an inline element (like a span) that don't extend all the way across the page like a block level element would and use a mouseover event handler on the span.

document.querySelector("span").addEventListener("mouseover", function(){
  console.log("You are over the text of the span");
});
<div>
  <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Trigger the event when on this text but not the blank space after.
  </span>
</div>

And, if you need a solution that only employs JavaScript, we can change the display property for a div so that it flows as an inline element:

const div = document.querySelector("div");

div.style.display = "inline"; // Make the div act like a span

div.addEventListener("mouseover", function(){
  console.log("You are over the text of the div that is acting as an inline element");
});
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Trigger the event when on this text but not the blank space after.
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!