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

312
Visualizações
How to make jQuery function execute only once, even if we declare it multiple times in a form?

I have a jQuery function when you click a button with particular class in a document, it will execute something. Ex. below.

$(document).on('click', '.tambah', function(){
  alert('tombol tambah telah ditekan');
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="btn btn-md btn-success tambah">+ tambah</button>

Lets say they are in the same script called 'script.php'. When I called the script once, the function of clicking that class will be executed once, but when I called the script again, when I clicked the button it executed twice. How to prevent it from executing more than one?

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

0

If you are calling a function that performs the binding, then it can stack up. In a case like this you would usually "unbind" first

$(document).unbind('click').on('click', '.tambah', function(){
  alert('tombol tambah telah ditekan');
});
about 4 years ago · Juan Pablo Isaza Relatório

0

event.stopImmediatePropagation(); [docs] should do the trick

$(document).on('click','.tambah',function(event){
    alert('tombol tambah telah ditekan');
    event.stopImmediatePropagation();
});
$(document).on('click','.tambah',function(event){
    alert('tombol tambah telah ditekan');
    event.stopImmediatePropagation();
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="btn btn-md btn-success tambah">+ tambah</button>

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