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

117
Visualizações
JQUERY Click event doesn't work only at the first click call

In the code, I try to print the name that is saved as a data value for each button.. but on the first click event call it doesn't work. while after the first time it works normally.

NOTE: off('click') function it is because without it this function fires multiple times with the following rate: 1times on a click, 2times on the next click, 3times on the next click, 4times on the next click, ...... and so on.

So it(off('click')) prevents this to happen.

$(document).on('click', '.btn-add', () => {

   $(".btn-add").off('click').on('click', (obj) => {

      const nameOfProduct = obj.currentTarget.value;

      console.log("name selected = " + nameOfProduct);

   })

})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is probably what you want to do, after document is ready, add button click handler:

 $(document).ready(() => {
    $(".btn-add").on('click', (obj) => {

      const nameOfProduct = obj.currentTarget.value;

      console.log("name selected = " + nameOfProduct);

   })
 })

Your code removes handler then adds handler to the button click after every click on button.. So after first click, you try to remove, then you add handler. After that you fire handler function, remove handler and add handler, which is probably not what you want to do.

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