Home > Software engineering >  How to pass Django formset form variable to JavaScript function
How to pass Django formset form variable to JavaScript function

Time:06-27

Basically I am using formsets, each form has a form ID and a deleted button amongst other things. When the delete button is clicked i'd like to call a function and pass the form ID to it, how can i achieve this?

{% for form in formset %}

{{ form.form_id_test }}

<button id="delete-form" type="button"  
onclick="deleteFormFunc({{ form.form_id_test }})">
    Delete Form
</button>

{% endfor %}

I am not able to pass the arguments like this, basically syntax error.

CodePudding user response:

You can obtain the form id of a formset form with form.id, eg,

{% for form in formset %}
    ...onclick="deleteFormFunc('{{ form.id }}')">...
{% endfor %}

...which should be a string value like 'id_form-1-id' that you can use in document.getElementById() in your deleteFormFunc

CodePudding user response:

    ...onclick="deleteFormFunc(this.id)">...

this.id will get the id of the tag it is encoded in.

  • Related