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

186
Visualizações
Detect double click on images in javascript

Any suggestion what is the most convenient way to detect if a double click event was not on text, but on images (or other elements on the websites)?

More info: at the moment, I use "document.addEventListener("dblclick", getSelectedText);" to get a double-clicked word on the website. But many users of my extension complain that they often mistakenly double click on images etc... so I want to eliminate those double-clicked events.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Instead of calling getSelectedText you could call a wrapping function that checks the target element's tag type:

function ignoreTargetImages (event) {
  if (event.target.tagName === 'IMG') {
    // element is an image, so handle differently or ignore
  } else {
    getSelectedText(event);
  }
}

document.addEventListener("dblclick", ignoreTargetImages);
about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a function which only calls your getSelectedText method if the double click was not on one or more ignored tags

function ignoreTags(ignoreTags, fn){
  return function(e){
    if(!ignoreTags.includes(e.target.tagName)) 
      fn(e);
  }
}

function getSelectedText(e) { console.log("get the text") }

document.addEventListener("dblclick", ignoreTags(["IMG"],getSelectedText));
<p>Some text</p>

<img src="https://via.placeholder.com/150">

This would allow you to add to the list of ignored tags should you need to.

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