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

222
Vistas
Popup-model is not opening in for loop (HTML)

I am trying to open a Pop up Model in for loop for every instance but the pop up model is opening for first instance But is not opening for other instances. I am using Django in template loop.

template.html

{% for comment in comments %}

    <button id="ModelButton">Open Model</button>
    <div id="Model" class="flagModel">
        <div class="modal-content">
            <div class="modal-mod">
                <div class="container-fluid">
                    <form method="post" enctype="multipart/form-data">
                        {% csrf_token %} 
                            {{form}}
                            <input type="submit">
                    </form>
                </div>
            </div>
        </div>
    </div>

<script>
    var formModel= document.getElementById("Model");
    var formButton = document.getElementById("ModelButton");
    formButton .onclick = function () {
      formModel.style.display = "block";
    }
    window.onclick = function (event) {
      if (event.target == formModel) {
        formModel.style.display = "none";
      }
    }
</script>

{% endfor %}

When I Place the JavaScript outside the for loop then it doesn't even work for one.

I have tried many times but it is still not working. Any help would be much Appreciated. Thank You

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

0

Specify unique IDs to the modals and pass the id number to a single function to open form modals on click of their respective buttons.

{% for comment in comments %}
    <button onclick="openModal({{forloop.counter}})">
        Open Model
    </button>

    <div id="Modal-{{forloop.counter}}" class="flagModel">
        <div class="modal-content">
            <div class="modal-mod">
                <div class="container-fluid">
                    <form method="post" enctype="multipart/form-data">
                        {% csrf_token %} 
                        {{form}}
                        <input type="submit">
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endfor %}

<script>
    function openModal(id) {
        var formModal = document.getElementById(`Modal-${id}`);
        if (formModal) {
            formModal.style.display = "block";
        }
    }
    
    window.onclick = function (event) {
      if (event.target == formModel) {
        formModel.style.display = "none";
      }
    }
</script>
about 4 years ago · Juan Pablo Isaza Denunciar

0

The id should be unique on a page, you should use class in loop instead of id,

Here I have updated your script that may help you

{% for comment in comments %}

    <button class="ModelButton">Open Model</button>
    <div class="flagModel Model">
        <div class="modal-content">
            <div class="modal-mod">
                <div class="container-fluid">
                    <form method="post" enctype="multipart/form-data">
                        {% csrf_token %} 
                            {{form}}
                            <input type="submit">
                    </form>
                </div>
            </div>
        </div>
    </div>

<script>
    var formButton = document.querySelectorAll(".ModelButton");
    formButton.forEach(function(element) {
      element.addEventListener('click', function(eventObj) {
        var formModel = eventObj.currentTarget.nextElementSibling;
        formModel.style.display = "block";
        formModel.addEventListener('click', function(event) {
          eventObj.currentTarget.style.display = "none";
        });
      })
    });
    
</script>

{% endfor %}
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