Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

313
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda