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

184
Views
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 answers
Answer question

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 Report

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 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!