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

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

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