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

153
Vistas
Is there a Javascript function to restrict a line in a function to execute one time only

Problem: I have a page which has multiple save button and an alert message will pop out if the button is pressed.I have a saveAll button which is using nodeList to save all data based on whether the checkbox is checked, meaning if 10 checkbox is checked then the saveall() function will execute the save() function 10 times and 10 alert message will pop out.

What I expected: I want to run the saveAll() function and the alert message should only pop out one time so I am expecting a function to restrict the alert in save() function to execute one time only even though it will be executed 10 times continuously. Any other way to get the expecting result is appreciated.

Example Code:

<table class="table" border="1">
      <thead>
        <tr>
          <th>Input</th>
          <th><input id="1"></th>
          <th><input type="checkbox" class="box"></th>
          <th><button type="submit" onclick="save()">save</button></th>
        </tr>
        <tr>
          <th>Input</th>
          <th><input id="2"></th>
          <th><input type="checkbox"  class="box"></th>
          <th><button onclick="save()">save</button></th>
        </tr>
        <tr>
          <th>Input</th>
          <th><input id="3"></th>
          <th><input type="checkbox"  class="box"></th>
          <th><button onclick="save()">save</button></th>
        </tr>
        <button onclick="saveall()">save all</button>
      </thead>
    </table>

function save(){
    //save something into database
     alert("success");
}
       
function saveall() {
        let nodeList = document.querySelectorAll('.box:checked');
        nodeList.forEach(node => node.parentNode.parentNode.querySelectorAll('button')[0].click());
}  
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

this is happening due to event bubbling hence stopped using event.stopPropagation.

explained with example in https://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing#:~:text=Event%20bubbling%20and%20capturing%20are,the%20elements%20receive%20the%20event.

about 4 years ago · Juan Pablo Isaza Denunciar

0

function saveall(event) {
    let nodeList = document.querySelectorAll('.box:checked');
    nodeList.forEach(node => node.parentNode.parentNode.querySelectorAll('button')[0].click());
    event.stopPropagation();
} 
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