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

131
Vistas
Adding event listeners for dynamic items in an MVC structure?

I know how to add event listeners to dynamically created elements, but I'm having a bit of a mental block doing it in an MVC pattern.

So my controller tells the view to open a modal with several elements/cards. Each card has a button. Pressing the button replaces the card with another newly created card. Something like this:

// Controller.js

if(response.data) {
    view.renderModal(response.data);
    this.addModalListeners();
}
...
function addModalListeners() {
    // Select the elements' btns, loop over them adding logic
    const btns = document.querySelector('.btns');
    btns.forEach(btn => {
        btn.addEventListener('click', () => {
            // do things here that the element needs
            view.replaceTheElementThatContainsThisButton(// pass that info);
        })
    })
}

So basically my problem is the Controller sets the logic for the btns clicks. But the view is responsible for replacing the element.

  • I could add a listener to the new element's button in the view, but then I don't have access to the data in the Controller.

  • I could also create the new element in the Controller, add the listener, and then pass it to the view, but creating a DOM element doesn't feel like the Controller's responsibility?

I'm sure there's an obvious solution, but I can't yet see it

Any help would be appreciated

*Edit: I guess I could also have the logic in a separate function in the controller and pass it to the view? This feels like the best answer I can think of so far

about 4 years ago · Juan Pablo Isaza
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