# Moving javascript code and form outside a for loop

**URL:** <https://forum.djangoproject.com/t/moving-javascript-code-and-form-outside-a-for-loop/7751>\
**Category:** Using Django\
**Created:** [May 2, 2021, 3:07am UTC](https://forum.djangoproject.com/t/moving-javascript-code-and-form-outside-a-for-loop/7751 "2021-05-02T03:07:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rukcs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rukcs/32/4402_2.png) [@rukcs](https://forum.djangoproject.com/u/rukcs)\
**Post date:** [May 2, 2021, 3:07am UTC](https://forum.djangoproject.com/t/moving-javascript-code-and-form-outside-a-for-loop/7751/1 "2021-05-02T03:07:53Z")

</div>

Hi, I want to create a popup modal form when someone clicks on delete. I’m trying this thing I got from here : [How To Make a Modal Box With CSS and JavaScript (w3schools.com)](https://www.w3schools.com/howto/howto_css_modals.asp)

The problem is that I don’t want to repeat that form and js code on each for loop item, I want it outside the loop with a unique product pk or id on each click

here my html code:

```
{% extends 'base.html' %}

{% block content %}
<div class="container">
    {% if messages %}
    <ul class="messages">
        {% for message in messages %}
        <li {% if message.tags %} class=" {{ message.tags }} " {% endif %}> {{ message }} </li>
        {% endfor %}
    </ul>
    {% endif %}
    <div class="grid-container">
        {% for product in products %}
        <div class="grid-item">
            <div>
                <img src="{{ product.image_url.url }}" alt="{{ product.title }}">
                <h5>{{ product.title }}</h5>

                <!-- DELETE Trigger/Open The Modal -->
                <button id="myBtn">Open Modal</button>

            </div>
        </div>

        <div id="myModal" class="modal">
            <!-- Modal content -->
            <div class="modal-content">

                <span class="close">&times;</span>
                <h1>are you sure you want to delete: "{{product.title}}"؟</h1>
                <form class="form_container" enctype="multipart/form-data" action="{% url 'delete' product.pk %}"
                    method="post">
                    {% csrf_token %}
                    <label for="pincode">enter your code to delte:</label>
                    <input type="number" id="pincode" name="pincode">
                    <input type="submit" name="Confirm" value="delete">
                </form>
            </div>

        </div>

        <script>

            var modal = document.getElementById("myModal");

            // Get the button that opens the modal
            var btn = document.getElementById("myBtn");

            // Get the <span> element that closes the modal
            var span = document.getElementsByClassName("close")[0];

            // When the user clicks on the button, open the modal
            btn.onclick = function () {
                modal.style.display = "block";
            }

            // When the user clicks on <span> (x), close the modal
            span.onclick = function () {
                modal.style.display = "none";
            }

            // When the user clicks anywhere outside of the modal, close it
            window.onclick = function (event) {
                if (event.target == modal) {
                    modal.style.display = "none";
                }
            }

        </script>
        {% endfor %}
    </div>
</div>
{% endblock %}
```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 2, 2021, 1:03pm UTC](https://forum.djangoproject.com/t/moving-javascript-code-and-form-outside-a-for-loop/7751/2 "2021-05-02T13:03:02Z")

</div>

This is more a JavaScript issue than a Django issue - you may get more & better help in a JavaScript-related forum.

In general, you can add the data elements you’re going to need as [custom attributes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes) to the individual elements being rendered, then access them through the `this` reference for the event handler. This allows you to render one instance of the code and modal box, and update it from your JavaScript on an on-demand basis.

---

<div class="post-metadata">

**Author:** ![rukcs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rukcs/32/4402_2.png) [@rukcs](https://forum.djangoproject.com/u/rukcs)\
**Post date:** [May 3, 2021, 10:32pm UTC](https://forum.djangoproject.com/t/moving-javascript-code-and-form-outside-a-for-loop/7751/3 "2021-05-03T22:32:25Z")

</div>

I see , thanks for the tip I’ll try to see what to do I’m very low experienced in javascript
