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

152
Views
How to get the mouse position when click on a specific element?

For example, I would like the content of a text field to be copied to the clipboard when I click on it. A note should then be displayed at the mouse position of this click. This all works well as far as I can assign a class to the relevant text field or any element.

$(document).ready(function(){
    $(".click-n-copy").click(function (e) {
        $(this).select();
        navigator.clipboard.writeText($(this).text());
        displaySuccessMessage(e.pageX,e.pageY,"copied");
    })  
});


function displaySuccessMessage(x,y,text)
{
        msg_width = 80;
        x = x - (msg_width/2);
        $('#successMessageTemp').remove();
        $("body").append("<div id='successMessageTemp' style='text-align:center;vertical-align:middle;width:"+msg_width+"px;position:absolute;top:"+y+"px;left:"+x+"px;border:0px solid grey;background:white;margin:2px;display:none'>" + text + "</div>");
        $("#successMessageTemp").html(text);      
        $("#successMessageTemp").show('slow');
        setTimeout('$("#successMessageTemp").hide("slow")',2000);
}

Now I would now like to do the same for words in markdown texts (standard markdown = no classes possible for links).

So, I can turn those words into links and call a function that does everything (copy, show message).

But what is the best way to do it? I need the mouse position at the time of the click usable in this function. Does this mean that I have to make eventlistener for all click events and mousemoves just to catch a few possible clicks?

Or is there a better way? Can I query the mouse position once as a snapshot instead of tracking it permanently?

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

0

Try to use this :

function printMousePos(event) {
  console.log("clientX: " + event.clientX + - clientY: " + event.clientY);
}

document.addEventListener("click", printMousePos);

MouseEvent.clientX Read only The X coordinate of the mouse pointer in local (DOM content) coordinates.

MouseEvent.clientY Read only The Y coordinate of the mouse pointer in local (DOM content) coordinates.

For more details

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!