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

261
Views
How do I use 'click' and 'dbclick' events on one element?

I have a tag element for a web page. when clicking once, one logic is executed, when clicking twice, another. However, the DOM only responds to one click and fires immediately, no matter how quickly you try to fire a double click event.

How do I use these two events on one element?

export const createTag = (name) => {
  const tag = document.createElement('span');
  tag.innerHTML = name;

  tag.addEventListener('dblclick', () => {
    tags.removeChild(tag);
    storeTags.splice(storeTags.indexOf(name), 1);
  });

  tag.addEventListener('click', () => {
    search.value = tag.innerHTML;
    const keyboardEvent = new KeyboardEvent('keypress', {
      code: 'Enter',
      key: 'Enter',
      charCode: 13,
      keyCode: 13,
      view: window,
      bubbles: true,
    });
    storeTags.splice(storeTags.indexOf(name), 1);
    storeTags.unshift(name);
    search.dispatchEvent(keyboardEvent);
  });

  return tag;
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The basic idea to achieve this would be to add some small delay to the click operation, and cancel that if a dblclick is received. This does lead to reduced responsiveness of your UI, as your application is now having to explicitly distinguish between single and double clicks in a kind of "wait and see" approach. This can also be unreliable as the user may set any length of time as their double-click threshold in their device's Accessibility settings.

However, if these issues are deemed not to be enough of a concern, the "best of the worst" way to do this would be to manually implement double-click checks.

export const createTag = (name) => {
  const tag = document.createElement('span');
  tag.innerHTML = name;

  const clickHander = () => {
    search.value = tag.innerHTML;
    const keyboardEvent = new KeyboardEvent('keypress', {
      code: 'Enter',
      key: 'Enter',
      charCode: 13,
      keyCode: 13,
      view: window,
      bubbles: true,
    });
    storeTags.splice(storeTags.indexOf(name), 1);
    storeTags.unshift(name);
    search.dispatchEvent(keyboardEvent);
  };

  const dblClickHandler = () => {
    tags.removeChild(tag);
    storeTags.splice(storeTags.indexOf(name), 1);
  };

  let clickTimer;
  tag.addEventListener('click', () => {
    if( clickTimer) {
      clearTimeout(clickTimer);
      dblClickHandler();
      clickTimer = null;
    }
    else {
      clickTimer = setTimeout(() => {
        clickHandler();
        clickTimer = null;
      }, 500);
    }
  });

  return tag;
};

Adjust the timer as you feel appropriate. Smaller numbers will lead to more responsive clicks, but will make double-clicking harder for people with reduced mobility.

I would really strongly recommend using a different UI here. Overloading clicks in this way is a really bad idea in general.

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!