Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda