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

139
Visualizações
How to handle a onClick inside an a element

There are a list of elements like this:

    <a href={productPath} target={openInNewTab ? '_blank' : '_self'}>
      <figure>
        <img src={imageBaseUrl() + img} alt={product.title} />
      </figure>
      {isFavorite ? (
        <div className="remove-favorite" onClick={() => deleteCallBack(product.id)}>
         Delete
        </div>
      ) : null}
    </a>

but when I want to click on div element, but my href is clicked.

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

0

You can do stopPropagation().

{isFavorite ?
    <div
    className="remove-favorite"
    onClick={
      (event) => {
        event.stopPropagation();
        event.preventDefault();
        deleteCallBack(product.id);
      }
    }
  >
    Delete
  </div>
) : null}

stopPropagation prevents further propagation of the current event in the capturing and bubbling phases.

preventDefault prevents the default action the browser makes on that event.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use event.stopPropagation()

<div className="remove-favorite" onClick={(e) => {
   e.stopPropagation()
   deleteCallBack(product.id)
}}>
   Delete
</div>
about 4 years ago · Juan Pablo Isaza Relatório

0

You should add into the link onClick event:

onClick={(event) => {
    event.stopPropagation();
    event.preventDefault();
}
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