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

110
Vistas
Use one() in inline onclick attribute

I have a function that is triggered on an inline onclick.

What I want is for that function to get called only once but don't know how to do that with an inline onclick. It's working at the moment but runs everytime the user clicks rather than just once.

This is how I have it:

HTML

<div class= "container" onclick="modal('#modal')"></div>

I have tried having it as a Jquery function as follows and remove the above click but that is not working either:

$(".container").on( "click", modal( '#modal') {
  alert( "The event happened!" );
});

Any idea how to make it so the function only runs once?

Thank you

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you wish to use jQuery's one(), you'll need to remove the onclick from the HTML, and change the JS to read:

$( ".container" ).one( "click", function() {
  alert( "The event happened!" );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class= "container">Container</div>

This way, the alert() will only show on the first click.


If you wish to use the on() as stated in your question, use $(this).off('click'); to remove the event listener after the fist press:

$( ".container" ).on( "click", function() {
  alert( "The event happened!" );
  $(this).off('click');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class= "container">Container</div>

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