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

125
Vistas
How to observe one element with mutationObserver for multiple elements in an array in a for loop?

I am working on a chrome extension where I have to loop through an array of buttons to click.

Whenever a click on a connect button happens, a modal is appeared in the DOM to ask for a confirmation, and a send button needs to be clicked before moving on to the next button in the array.

The general flow is:

  1. Loop through the array of buttons
  2. Click the connect button
  3. Wait for the modal to appear
  4. Click on Send button on modal
  5. Move on to the next button in the array

I am using mutationObserver() to wait for the modal to appear before firing off a click event on the Send button.

However, it only works once for the first element, and the loop stops.

Here's my code:

async function connect(): Promise<void> {
  const connectBtn = document.querySelectorAll<HTMLElement>(
    ".connect"
  );

  const observer = new MutationObserver((mutations) => {
    const send = document.querySelectorAll<HTMLElement>(
      ".send"
    )[0];
    if (send.textContent === "Send") {
      send.click();
    }
  });

  for (const btn of connectBtn) {
    if (btn.textContent === "Connect") {
      btn.click();
      observer.observe(
        document.querySelector("#modal") as HTMLElement,
        {
          childList: true,
          subtree: true,
          characterData: true,
        }
      );

    }
  }

  observer.disconnect();
}
about 4 years ago · Santiago Trujillo
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